title: Handle Server Response in Android
body: <p>In my application I am fetching data using webservices.But when the server is down or not in active state it gives response code 500 and my application force close.</p>
  
  <p>Please guide me how to handle this condition in my application.The logcat output is shown below :</p>
  
  <pre><code>12-05 12:17:22.337: V/response code(950): 500
  12-05 12:17:22.347: W/System.err(950): org.json.JSONException: A JSONArray text must start with '[' at character 1 of &lt;html&gt;
  
  12-05 12:17:22.347: W/System.err(950):     &lt;head&gt;
  
  12-05 12:17:22.347: W/System.err(950):         &lt;title&gt;Configuration Error&lt;/title&gt;
  
  12-05 12:17:22.347: W/System.err(950):         &lt;style&gt;
  
  12-05 12:17:22.347: W/System.err(950):          body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;} 
  
  12-05 12:17:22.347: W/System.err(950):          p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px}
  
  12-05 12:17:22.347: W/System.err(950):          b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px}
  
  12-05 12:17:22.347: W/System.err(950):          H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red }
  
  12-05 12:17:22.347: W/System.err(950):          H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon }
  
  12-05 12:17:22.356: W/System.err(950):          pre {font-family:"Lucida Console";font-size: .9em}
  
  12-05 12:17:22.356: W/System.err(950):          .marker {font-weight: bold; color: black;text-decoration: none;}
  
  12-05 12:17:22.356: W/System.err(950):          .version {color: gray;}
  
  12-05 12:17:22.356: W/System.err(950):          .error {margin-bottom: 10px;}
  
  12-05 12:17:22.356: W/System.err(950):          .expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; }
  
  12-05 12:17:22.356: W/System.err(950):         &lt;/style&gt;
  
  12-05 12:17:22.356: W/System.err(950):     &lt;/head&gt;
  
  12-05 12:17:22.356: W/System.err(950): 
  
  12-05 12:17:22.356: W/System.err(950):     &lt;body bgcolor="white"&gt;
  
  12-05 12:17:22.366: W/System.err(950): 
  
  12-05 12:17:22.366: W/System.err(950):             &lt;span&gt;&lt;H1&gt;Server Error in '/' Application.&lt;hr width=100% size=1 color=silver&gt;&lt;/H1&gt;
  
  12-05 12:17:22.366: W/System.err(950): 
  
  12-05 12:17:22.366: W/System.err(950):             &lt;h2&gt; &lt;i&gt;Configuration Error&lt;/i&gt; &lt;/h2&gt;&lt;/span&gt;
  
  12-05 12:17:22.366: W/System.err(950): 
  
  12-05 12:17:22.366: W/System.err(950):             &lt;font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif "&gt;
  
  12-05 12:17:22.366: W/System.err(950): 
  
  12-05 12:17:22.366: W/System.err(950):             &lt;b&gt; Description: &lt;/b&gt;An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.
  
  12-05 12:17:22.377: W/System.err(950):             &lt;br&gt;&lt;br&gt;
  
  12-05 12:17:22.377: W/System.err(950): 
  
  12-05 12:17:22.377: W/System.err(950):             &lt;b&gt; Parser Error Message: &lt;/b&gt;Unrecognized attribute 'targetFramework'. Note that attribute names are case-sensitive.&lt;br&gt;&lt;br&gt;
  
  12-05 12:17:22.377: W/System.err(950): 
  
  12-05 12:17:22.377: W/System.err(950):             &lt;b&gt;Source Error:&lt;/b&gt; &lt;br&gt;&lt;br&gt;
  
  12-05 12:17:22.377: W/System.err(950): 
  
  12-05 12:17:22.377: W/System.err(950):             &lt;table width=100% bgcolor="#ffffcc"&gt;
  
  12-05 12:17:22.377: W/System.err(950):                &lt;tr&gt;
  
  12-05 12:17:22.377: W/System.err(950):                   &lt;td&gt;
  
  12-05 12:17:22.377: W/System.err(950):                       &lt;code&gt;&lt;pre&gt;
  
  12-05 12:17:22.377: W/System.err(950): 
  
  12-05 12:17:22.377: W/System.err(950): Line 30:             &amp;lt;error statusCode=&amp;quot;404&amp;quot; redirect=&amp;quot;filenotfound.htm&amp;quot; /&amp;gt;
  
  12-05 12:17:22.377: W/System.err(950): Line 31:         &amp;lt;/customErrors&amp;gt;
  
  12-05 12:17:22.377: W/System.err(950): &lt;font color=red&gt;Line 32:         &amp;lt;compilation debug=&amp;quot;true&amp;quot; targetFramework=&amp;quot;4.0&amp;quot;&amp;gt;
  
  12-05 12:17:22.396: W/System.err(950): &lt;/font&gt;Line 33:          &amp;lt;expressionBuilders&amp;gt;
  
  12-05 12:17:22.396: W/System.err(950): Line 34:                 &amp;lt;add expressionPrefix=&amp;quot;NopResources&amp;quot; type=&amp;quot;NopSolutions.NopCommerce.BusinessLogic.Localization.NopResourceExpressionBuilder, Nop.BusinessLogic&amp;quot; /&amp;gt;&lt;/pre&gt;&lt;/code&gt;
  
  12-05 12:17:22.396: W/System.err(950): 
  
  12-05 12:17:22.396: W/System.err(950):                   &lt;/td&gt;
  
  12-05 12:17:22.396: W/System.err(950):                &lt;/tr&gt;
  
  12-05 12:17:22.396: W/System.err(950):             &lt;/table&gt;
  
  12-05 12:17:22.396: W/System.err(950): 
  
  12-05 12:17:22.396: W/System.err(950):             &lt;br&gt;
  
  12-05 12:17:22.396: W/System.err(950): 
  
  12-05 12:17:22.396: W/System.err(950):             &lt;b&gt; Source File: &lt;/b&gt; C:\Websites\balajeebazaar\web.config&lt;b&gt; &amp;nbsp;&amp;nbsp; Line: &lt;/b&gt; 32
  
  12-05 12:17:22.396: W/System.err(950):             &lt;br&gt;&lt;br&gt;
  
  12-05 12:17:22.396: W/System.err(950): 
  
  12-05 12:17:22.396: W/System.err(950):             &lt;hr width=100% size=1 color=silver&gt;
  
  12-05 12:17:22.407: W/System.err(950): 
  
  12-05 12:17:22.407: W/System.err(950):             &lt;b&gt;Version Information:&lt;/b&gt;&amp;nbsp;Microsoft .NET Framework Version:2.0.50727.4963; ASP.NET Version:2.0.50727.4955
  
  12-05 12:17:22.407: W/System.err(950): 
  
  12-05 12:17:22.407: W/System.err(950):             &lt;/font&gt;
  
  12-05 12:17:22.407: W/System.err(950): 
  
  12-05 12:17:22.416: W/System.err(950):     &lt;/body&gt;
  
  12-05 12:17:22.416: W/System.err(950): &lt;/html&gt;
  
  12-05 12:17:22.416: W/System.err(950): &lt;!-- 
  
  12-05 12:17:22.416: W/System.err(950): [ConfigurationErrorsException]: Unrecognized attribute 'targetFramework'. Note that attribute names are case-sensitive. (C:\Websites\balajeebazaar\web.config line 32)
  
  12-05 12:17:22.416: W/System.err(950):    at System.Web.HttpRuntime.HostingInit(HostingEnvironmentFlags hostingFlags)
  
  12-05 12:17:22.416: W/System.err(950): [HttpException]: Unrecognized attribute 'targetFramework'. Note that attribute names are case-sensitive. (C:\Websites\balajeebazaar\web.config line 32)
  
  12-05 12:17:22.416: W/System.err(950):    at System.Web.HttpRuntime.FirstRequestInit(HttpContext context)
  
  12-05 12:17:22.416: W/System.err(950):    at System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context)
  
  12-05 12:17:22.416: W/System.err(950):    at System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context)
  
  12-05 12:17:22.416: W/System.err(950): --&gt;&lt;!-- 
  
  12-05 12:17:22.426: W/System.err(950): This error page might contain sensitive information because ASP.NET is configured to show verbose error messages using &amp;lt;customErrors mode="Off"/&amp;gt;. Consider using &amp;lt;customErrors mode="On"/&amp;gt; or &amp;lt;customErrors mode="RemoteOnly"/&amp;gt; in production environments.--&gt;
  12-05 12:17:22.426: W/System.err(950):  at org.json.JSONTokener.syntaxError(JSONTokener.java:448)
  12-05 12:17:22.426: W/System.err(950):  at org.json.JSONArray.&lt;init&gt;(JSONArray.java:104)
  12-05 12:17:22.426: W/System.err(950):  at org.json.JSONArray.&lt;init&gt;(JSONArray.java:150)
  12-05 12:17:22.426: W/System.err(950):  at cm.bzaar.CallWebservices.getCategory(CallWebservices.java:48)
  12-05 12:17:22.426: W/System.err(950):  at cm.bzaar.CategoryActivity$Loader.doInBackground(CategoryActivity.java:98)
  12-05 12:17:22.426: W/System.err(950):  at cm.bzaar.CategoryActivity$Loader.doInBackground(CategoryActivity.java:1)
  12-05 12:17:22.436: W/System.err(950):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
  12-05 12:17:22.436: W/System.err(950):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:256)
  12-05 12:17:22.436: W/System.err(950):  at java.util.concurrent.FutureTask.run(FutureTask.java:122)
  12-05 12:17:22.436: W/System.err(950):  at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:648)
  12-05 12:17:22.436: W/System.err(950):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:673)
  12-05 12:17:22.436: W/System.err(950):  at java.lang.Thread.run(Thread.java:1060)
  12-05 12:17:22.436: W/dalvikvm(950): threadid=17: thread exiting with uncaught exception (group=0x4001aa28)
  12-05 12:17:22.446: E/AndroidRuntime(950): Uncaught handler: thread AsyncTask #1 exiting due to uncaught exception
  12-05 12:17:22.446: E/AndroidRuntime(950): java.lang.RuntimeException: An error occured while executing doInBackground()
  12-05 12:17:22.446: E/AndroidRuntime(950):  at android.os.AsyncTask$3.done(AsyncTask.java:200)
  12-05 12:17:22.446: E/AndroidRuntime(950):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:234)
  12-05 12:17:22.446: E/AndroidRuntime(950):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:258)
  12-05 12:17:22.446: E/AndroidRuntime(950):  at java.util.concurrent.FutureTask.run(FutureTask.java:122)
  12-05 12:17:22.446: E/AndroidRuntime(950):  at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:648)
  12-05 12:17:22.446: E/AndroidRuntime(950):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:673)
  12-05 12:17:22.446: E/AndroidRuntime(950):  at java.lang.Thread.run(Thread.java:1060)
  12-05 12:17:22.446: E/AndroidRuntime(950): Caused by: java.lang.NullPointerException
  12-05 12:17:22.446: E/AndroidRuntime(950):  at cm.bzaar.CategoryActivity$Loader.doInBackground(CategoryActivity.java:100)
  12-05 12:17:22.446: E/AndroidRuntime(950):  at cm.bzaar.CategoryActivity$Loader.doInBackground(CategoryActivity.java:1)
  12-05 12:17:22.446: E/AndroidRuntime(950):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
  12-05 12:17:22.446: E/AndroidRuntime(950):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:256)
  12-05 12:17:22.446: E/AndroidRuntime(950):  ... 4 more
  </code></pre>
  
  <p><strong>SEE FOLLOWING CODE :</strong></p>
  
  <p><strong>code segment where function is called to call webservice :</strong></p>
  
  <pre><code>@Override
          protected Void doInBackground(Void... arg0) {
              CategoryArray = new JSONArray();
              CategoryArray = CW
                      .getCategory("url");
  
              for (int i = 0; i &lt;= CategoryArray.length() - 1; i++) {
                  try {
                      Log.v("category array : ", CategoryArray.getString(i));
                      String[] val = CategoryArray.getString(i).split("•");
                      CategoryID.add(i, val[0]);
                      CategoryList.add(i, val[1]);
                  } catch (JSONException e) {
                      e.printStackTrace();
                  }
              }
              adapter = new CategoryListAdapter(CategoryActivity.this,
                      CategoryList);
              return null;
          }
  </code></pre>
  
  <p><strong>function to call webservice :</strong></p>
  
  <pre><code>public JSONArray getCategory(String URL){
  
  try {
              HttpPost request = new HttpPost(URL);
              request.setHeader("Accept", "application/json");
              request.setHeader("Content-type", "application/json");
              // Send request to WCF service
              DefaultHttpClient httpClient1 = new DefaultHttpClient();
              HttpResponse response = httpClient1.execute(request);
              Log.v("response code", response.getStatusLine().getStatusCode()
                      + "");
              responseCode = response.getStatusLine().getStatusCode();
  //          if(responseCode &gt; 200){                   // HERE I WS TRYING TO BREAK THE CODE AND TRYING TO RETURN NULL BT UNABLE TO DO SO
  //              CategoryArray = new JSONArray();
  //              CategoryArray.put(false);
  //              return null;
  //          }else{
              HttpEntity responseEntity = response.getEntity();
              // Read response data into buffer
              char[] buffer = new char[(int) responseEntity.getContentLength()];
              InputStream stream = responseEntity.getContent();
              InputStreamReader reader = new InputStreamReader(stream);
              reader.read(buffer);
              stream.close();
              CategoryArray = new JSONArray(new String(buffer));
              Log.v("results length : ", CategoryArray.length() + "");
  //          }
          } catch (Exception e) {
              // TODO: handle exception
              e.printStackTrace();
          }
          return CategoryArray;
  }
  </code></pre>
  
  <p>see the line where i am comparing response code.Please guide me on how can i break my code there and show u a toast on ui</p>