Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use ProgressBar to show a spinner while waiting a httpResponse from server
    primarykey
    data
    text
    <p>I'm trying to show a loading icon to the user when the application makes a query to online Database. I've tried using a AnimationDrawable (I gave up because there was no need of a custom icon), ProgressDialog and ProgressBar. The ProgressBar seems most appropriate, since I don't want a message, just a spinning icon. But I can not even make a ProgressBar appear on the screen, doesn't matter where I call it. I've got the ProgressDialog appearing in screen, but it only appears after the server's response, and if I use dismiss() or cancel() it doesn't even appear at all.</p> <p>I've had any success using AsyncTasks or Threads.</p> <p>In the app, there is a class JogarActivity.java that attemps to show a list of options. It receives some parameters like the user id, and calls UserFunctions:</p> <p>public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);</p> <pre><code> setContentView(R.layout.jogar_layout); Intent in = getIntent(); String url = this.getString(R.string.urlSite); ArrayList&lt;HashMap&lt;String, String&gt;&gt; respostaList = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;(); String idt = in.getStringExtra(TAG_ID); primeiraPergunta = in.getBooleanExtra(TAG_PRIMEIRAPERGUNTA, true); TextView insertPergunta = (TextView) findViewById(R.id.insertPergunta); ListView insertRespostas = (ListView) findViewById(R.id.listResposta); SharedPreferences settings = getSharedPreferences("PREFS_LOGIN", MODE_PRIVATE); Integer idUsuario = settings.getInt("idUsuario", 0); String idUser = idUsuario.toString(); if (primeiraPergunta){ UserFunctions userFunction = new UserFunctions(); json = userFunction.getJogar(idt, idUser); }else{ try { json = new JSONArray(in.getStringExtra(TAG_JSON)); json = json.getJSONArray(2); } catch (JSONException e) { e.printStackTrace(); } } </code></pre> <p>Below is the getJogar function in userFunctions: public JSONArray getJogar(String categoria, String usuarioId){ List params = new ArrayList();</p> <pre><code> params.add(new BasicNameValuePair("categoria", categoria)); params.add(new BasicNameValuePair("idUsuario", usuarioId)); JSONArray json = jsonParser.getJSONFromUrl(perguntaURL, params); return json; } </code></pre> <p>JSONParser.java is the class that makes the httpRequest: public JSONArray getJSONFromUrl(String url, List params) {</p> <pre><code> // Making HTTP request try { // defaultHttpClient HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); httpPost.setEntity(new UrlEncodedFormEntity(params)); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); // json = EntityUtils.toString(httpEntity); // HttpEntity httpEntity2 = httpEntity; json = EntityUtils.toString(httpEntity); // is = httpEntity.getContent(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } //then makes the JSON manipulation </code></pre> <p>As long as JSONParser and userFunctions aren't activities, I couldn't use ProgressDialogs inside them (Can't get the application context). All server requests are made in JSONParser.java, that's why i've first tried to put the ProgressDialog/ProgressBar there.</p> <p>The closest that i've reached was using this code in JogarActivity (It shows the ProgressDialog, but after getting server's response. And if I use dismiss, it doesn't even appears)</p> <pre><code> final ProgressDialog loader = new ProgressDialog(JogarActivity.this); loader.show(); //...the if-else code i've pasted above loader.dismiss(); </code></pre> <p>Even using runOnUiThread it doesn't works! I'm getting without options...</p> <p>Thanks for all help.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload