title: Android - Validating JSON when getting a response from a server to avoid a JSONException
body: <p>In some of my applications that talk to a server and get a response using http I use json to format the data server side and when it gets to the device I use something similar to this code which I found on stackoverflow: </p>
  
  <pre><code>private class LoadData extends AsyncTask&lt;Void, Void, String&gt; 
  { 
  private  JSONArray jArray;
  private  String result = null;
  private  InputStream is = null;
  private String entered_food_name=choice.getText().toString().trim();
  protected void onPreExecute() 
  {
  }
  
  @Override
  protected String doInBackground(Void... params) 
  {
  try {
  ArrayList&lt;NameValuePair&gt; nameValuePairs = new            ArrayList&lt;NameValuePair&gt;();
  HttpClient httpclient = new DefaultHttpClient(); 
  HttpPost httppost = new HttpPost("http://10.0.2.2/food.php");
  nameValuePairs.add(new BasicNameValuePair("Name",entered_food_name));
  httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
  HttpResponse response = httpclient.execute(httppost);
  
  HttpEntity entity = response.getEntity();
   is = entity.getContent();
  
      //convert response to string
  
  BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"),8);
      StringBuilder sb = new StringBuilder();
      String line = null;
      while ((line = reader.readLine()) != null) {
          sb.append(line);
      }
  
      is.close();
  
  
      result =sb.toString();
      result = result.replace('\"', '\'').trim();
  
  }
  catch(Exception e){
      Log.e("log_tag", " connection" + e.toString());                     
  }
  
  
  return result;  
  
  }
  
  @Override
  protected void onPostExecute(String result) 
  {  
  try{
  
      String foodName="";
      int Description=0;
  
      jArray = new JSONArray(result); // here if the result is null an exeption will occur
      JSONObject json_data = null;
  
      for (int i = 0; i &lt; jArray.length(); i++) {
          json_data = jArray.getJSONObject(i);
          foodName=json_data.getString("Name");
          .
          .
          .
          .
          .
      } 
      catch(JSONException e){ 
          **// what i can do here to prevent my app from crash and 
          //  make toast " the entered food isnot available " ????**
          Log.e("log_tag", "parssing  error " + e.toString()); 
      }   
  }
  }
  </code></pre>
  
  <p>I tried to use the solution on the page where I found this code. heres the link by the way <a href="https://stackoverflow.com/questions/10587599/how-do-i-prevent-my-app-from-crashing-unexpectedly-force-close-when-using-js?rq=1">How do I prevent my app from crashing unexpectedly, &quot;force close&quot;, when using JSON data, and handle the exception instead?</a></p>
  
  <p>I tried checking my json response with online validators and the json returned is valid. Im thinking that when users are in a poor coverage area the connection momentarily breaks or something causes the json to be broken which causes a json exception even though I have tried the method suggested in the thread I posted in that link. So I would like to be able to validate the JSON when it gets to the phone so if its valid I can proceed and if not than I would like to try and fix it or at least not try to pass a broken and not properly formatted json array into one of the methods that causes the JSONException. </p>
  
  <p>Any tips on validating the string I get in the response to make sure its valid JSON would be awesome.</p>
  
  <p>Edit: added stack dump related to problem</p>
  
  <pre><code>org.json.JSONException: Value null at results of type org.json.JSONObject$1 cannot be     converted to JSONArray
  at org.json.JSON.typeMismatch(JSON.java:96)
  at org.json.JSONObject.getJSONArray(JSONObject.java:548)
  at it.cores.Activity$GetM.doInBackground(Activity.java:1159)
  at it.cores.Activity$GetM.doInBackground(Activity.java:1)
  at android.os.AsyncTask$2.call(AsyncTask.java:185)
  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
  at java.util.concurrent.FutureTask.run(FutureTask.java:138)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
  at java.lang.Thread.run(Thread.java:1019)
  </code></pre>