Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To get the <code>count</code> value, just do </p> <pre><code>int count = json.getJSONObject("all").getInt("count"); </code></pre> <p>(you should replace <code>"count"</code> by a final variable as you did with the other <code>JSONObject</code> in your code)</p> <p>Then add a <code>if</code> before you do </p> <pre><code>JSONArray questions = json.getJSONObject("all").getJSONArray("questions"); </code></pre> <p>But you won't be able to display your toast in the <code>doInBackground</code> method, you will have to return the result (a String) and display it in <code>onPostExecute()</code> . So far you don't return the right value, you return <code>TAG_QUESTION</code>, which must be a final String, meaning that the result of your processing is never used outside of the method.</p> <p><strong>UPDATE</strong></p> <p>I think you should do like that:</p> <p>First, you should not do all your processing in <code>doInBackground()</code>. In this method you should just retrieve your json, return it and process it in <code>onPostExecute()</code>. For that, you will need to change your <code>AsyncTask</code> so it looks like this (notice the change in the last parameter, from <code>String</code> to <code>JSONObject</code>):</p> <pre><code>private class yourAsyncTask extends AsyncTask&lt;String, Void, JSONObject&gt; { </code></pre> <p>This will tell your program that the return value of <code>doInBackground()</code> will be of <code>JSONObject</code> type. So what you have to do next is to change your <code>doInBackground()</code> so it looks like that (notice again the change in the return value, from <code>String</code> to <code>JSONObject</code> :</p> <pre><code>protected JSONObject doInBackground(String... args) { JSONObject json = new JSONObject(); try { Intent in = getIntent(); String searchTerm = in.getStringExtra("TAG_SEARCH"); String query = URLEncoder.encode(searchTerm, "utf-8"); String URL = "http://example.com"; JSONParsser jParser = new JSONParsser(); try { json = jParser.readJSONFeed(URL); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return json; } </code></pre> <p>Then change the type of the parameter in <code>onPostExecute()</code> to <code>JSONObject</code>, and do your processing after checking the <code>count</code> value:</p> <pre><code>@Override protected void onPostExecute(JSONObject json) { int count = json.getJSONObject("all").getInt("count"); if(count&gt;0) { // There are some questions to retrieve /* In this part I just copied what you put in your question, I don't know what your goal is so I can't help much, but you have the logic to do so */ JSONArray questions = json.getJSONObject("all").getJSONArray("questions"); for(int i = 0; i &lt; questions.length(); i++) { JSONObject question = questions.getJSONObject(i); String Subject = question.getString(TAG_QUESTION_SUBJECT); String ChosenAnswer = question.getString(TAG_QUESTION_CHOSENANSWER); String Content = question.getString(TAG_QUESTION_CONTENT); if (pDialog.isShowing()) pDialog.dismiss(); ListAdapter adapter = new SimpleAdapter(getBaseContext(), questionList, R.layout.listelements, new String[] { TAG_QUESTION_SUBJECT }, new int[] { R.id.Subject,}); setListAdapter(adapter); } else { Toast.makeText(ListView.this, "No data found", Toast.LENGTH_SHORT).show(); finish(); } } </code></pre>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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