Note that there are some explanatory texts on larger screens.

plurals
  1. POParsing through JSON to display error message
    primarykey
    data
    text
    <p>I want to add a toast message and to return the user to the main activity when my WEBSERVICE returns no data and allow me to explain more. When a user searches for a query, the webservice returns data to my JSON string that looks like this.</p> <pre><code>08-30 00:36:07.917: D/JSON String(1891): { 08-30 00:36:07.917: D/JSON String(1891): "all" : { 08-30 00:36:07.917: D/JSON String(1891): "count" : 25, 08-30 00:36:07.917: D/JSON String(1891): "questions" : [ { 08-30 00:36:07.917: D/JSON String(1891): "Id" : "20100728112033AAb4hTA", 08-30 00:36:07.917: D/JSON String(1891): "Subject" : "What is the oldest a bitch can more or less safely breed?", 08-30 00:36:07.917: D/JSON String(1891): "Content" : "Don't worry I'm not going to breed - both my bitches are getting neutered in the next year, as is my dog! Just that me and a friend were talking about it after talking to the man who gave me Misty (my border collie) who said she could still breed at 7 - I thought the oldest was 5?\n", 08-30 00:36:07.917: D/JSON String(1891): "Date" : "2010-07-28 18:20:33", 08-30 00:36:07.917: D/JSON String(1891): "Timestamp" : "1280341233", 08-30 00:36:07.917: D/JSON String(1891): "Link" : "http://answers.yahoo.com/question/?qid=20100728112033AAb4hTA", 08-30 00:36:07.917: D/JSON String(1891): "Type" : "Answered", 08-30 00:36:07.917: D/JSON String(1891): "CategoryId" : 396546021, 08-30 00:36:07.917: D/JSON String(1891): "CategoryName" : "Dogs", 08-30 00:36:07.917: D/JSON String(1891): "UserId" : "cP16Ctgxaa", 08-30 00:36:07.917: D/JSON String(1891): "UserNick" : "Kiko", 08-30 00:36:07.917: D/JSON String(1891): "UserPhotoURL" : "http://l.yimg.com/dg/users/1t1USxJpxAAEBQOGZjBMW0-5Wp_EG.medium.jpg", 08-30 00:36:07.917: D/JSON String(1891): "NumAnswers" : 8, 08-30 00:36:07.917: D/JSON String(1891): "NumComments" : 0, 08-30 00:36:07.917: D/JSON String(1891): "ChosenAnswer" : "You just \"imagined\" that ,dear...not \"thought\". &amp; your imagination is WRONG.\r\n\r\n9 or even 10,for a healthy TOP-PRODUCING bitc-h.\r\n\r\n\r\n\r\n*&amp;* bitches are SPAYED &amp; dogs are CASTRATED......big scary correct ADULT words.", 08-30 00:36:07.917: D/JSON String(1891): "ChosenAnswererId" : "clN6YITvaa", 08-30 00:36:07.917: D/JSON String(1891): "ChosenAnswererNick" : "Debunker", 08-30 00:36:07.917: D/JSON String(1891): "ChosenAnswerTimestamp" : "1280317077", 08-30 00:36:07.917: D/JSON String(1891): "ChosenAnswerAwardTimestamp" : "1280835336" 08-30 00:36:07.917: D/JSON String(1891): }, { </code></pre> <p>And here is my code of how I parse through this data to get the particular data that I want</p> <pre><code>protected String doInBackground(String... args) { 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(); JSONObject json = jParser.readJSONFeed(URL); try { 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); } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return TAG_QUESTION ; } </code></pre> <p>But when the user searches for a query and the webservice returns no results and by no results I mean the webservice data has <code>"count" : 0,</code> and it looks like this in my JSON string that looks like this.</p> <pre><code>09-02 00:25:55.466: D/JSON String(882): { 09-02 00:25:55.466: D/JSON String(882): "all" : { 09-02 00:25:55.466: D/JSON String(882): "count" : 0, 09-02 00:25:55.466: D/JSON String(882): "questions" : [ ] 09-02 00:25:55.466: D/JSON String(882): } 09-02 00:25:55.466: D/JSON String(882): } </code></pre> <p>When this happens I want an toast message to pop up and to return the user to the main activity but I just do not know how to parse through that data when it returns count 0. I already that I need to add a <code>if</code> statement in this location </p> <pre><code> try { // if statement here 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); </code></pre> <p>I need to know how to parse <code>"count" : 0,</code> so I can display the toast message and then return the user back to the main activity. Hope you see my idea here and let me know if you are having any trouble try to comprehend what I mean. P.S. if you wondering what <code>JSONParsser</code> is, it is just the class I use to connect to the webservice and <code>searchTerm</code> is me just passing <code>editText</code> and encoding it.</p> <pre><code>@Override protected void onPostExecute(String file_URL) { if(file_URL.equals("0")) { pDialog.dismiss(); Toast.makeText(ListView.this, "No data found", Toast.LENGTH_SHORT).show(); finish(); }else{ 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); } </code></pre> <p><strong>Update</strong> </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(); json = jParser.readJSONFeed(URL); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return json; } @Override protected void onPostExecute(JSONObject json) { int count = 0; try { count = json.getJSONObject("all").getInt("count"); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(count&gt;0) { pDialog.dismiss(); Toast.makeText(ListView.this, "No data found", Toast.LENGTH_SHORT).show(); finish(); }else{ try { 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); HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;(); map.put(TAG_QUESTION_SUBJECT, Subject); map.put(TAG_QUESTION_CONTENT, Content); map.put(TAG_QUESTION_CHOSENANSWER, ChosenAnswer); questionList.add(map); pDialog.dismiss(); ListAdapter adapter = new SimpleAdapter(getBaseContext(), questionList, R.layout.listelements, new String[] { TAG_QUESTION_SUBJECT }, new int[] { R.id.Subject,}); setListAdapter(adapter); } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } </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. 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