title: How to handle 500 Internal Server Error when using JSON
body: <p>I am relatively new to Android and I am using JSON to get data from a server. On the first loop at line 22, the StringBuilder contains, 500 Internal Server Error and then the jArray ends up coming back null. How can I handle this error?</p>
  
  <pre><code>public static JSONObject getJSON() {
      String jsonString = "";
      InputStream inStream = null;
  
      //http post
      JSONObject jArray = null;
      try {
          HttpClient httpClient = new DefaultHttpClient(new BasicHttpParams());
          HttpPost httpPost = new HttpPost(WS_URL);
          httpPost.setHeader("Content-type", "application/json");
  
          HttpResponse response = httpClient.execute(httpPost);
          HttpEntity entity = response.getEntity();
          inStream = entity.getContent();
  
          BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, "UTF-8"), 8);
          StringBuilder sb = new StringBuilder();
  
          String line = null;
  
          while ((line = reader.readLine()) != null) {
              sb.append(line + "\n");
          }
          inStream.close();
          jsonString = sb.toString();
  
          jArray = new JSONObject(jsonString);
  
  
          //outputTransactions(jArray);
  
      } catch (IOException e) {
          e.printStackTrace();
      } catch (JSONException e) {
          e.printStackTrace();
      }
  
      return jArray;
  }
  </code></pre>