Search code examples
androidtimeoutandroid-async-http

Android Application Timeout even after AsyncHttpClient timeout set


My app gets timeout even after timeout set. Please see below:

    final int DEFAULT_TIMEOUT = 200000 * 1000000000;
 // Make RESTful webservice call using AsyncHttpClient object
     AsyncHttpClient client = new AsyncHttpClient();
    client.setTimeout(DEFAULT_TIMEOUT);
     //client.get("http://182.188.33.27:9009/useraccount/login/dologin",params ,new AsyncHttpResponseHandler() {
    client.get("http://koha.cdtl.com.sg/cgi-bin/koha/ilsdi.pl?service=AuthenticatePatron",params ,new AsyncHttpResponseHandler() {
         // When the response returned by REST has Http response code '200'
         @Override
         public void onSuccess(String response) {
             // Hide Progress Dialog
             prgDialog.hide();
             try {
                     // JSON Object
                     JSONObject obj = new JSONObject(response);
                     // When the JSON response has status boolean value assigned with true
                     if(obj.getBoolean("status")){
                         Toast.makeText(getApplicationContext(), "You are successfully logged in!", Toast.LENGTH_LONG).show();
                         // Navigate to Home screen
                         navigatetoHomeActivity();
                     } 
                     // Else display error message
                     else{
                         errorMsg.setText(obj.getString("error_msg"));
                         Toast.makeText(getApplicationContext(), obj.getString("error_msg"), Toast.LENGTH_LONG).show();
                     }
             } catch (JSONException e) {
                 // TODO Auto-generated catch block
                 Toast.makeText(getApplicationContext(), "Error Occured [Server's JSON response might be invalid]!", Toast.LENGTH_LONG).show();
                 e.printStackTrace();

             }
         }
         // When the response returned by REST has Http response code other than '200'
         @Override
         public void onFailure(int statusCode, Throwable error,
             String content) {
             // Hide Progress Dialog 
             prgDialog.hide();
             // When Http response code is '404'
             if(statusCode == 404){
                 Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
             } 
             // When Http response code is '500'
             else if(statusCode == 500){
                 Toast.makeText(getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
             } 
             // When Http response code other than 404, 500
             else{
                 Toast.makeText(getApplicationContext(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet or remote server is not up and running]", Toast.LENGTH_LONG).show();
             }
         }
     });

It is strange, sometimes it shows desired result:

Toast.makeText(getApplicationContext(), "You are successfully logged in!", Toast.LENGTH_LONG).show();

But mostly:

Toast.makeText(getApplicationContext(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet or remote server is not up and running]", Toast.LENGTH_LONG).show();

I have tried to clean and rebuild the project too, but it did not help.

Any suggestions are welcome.

Thanks


Solution

  • In the end, managed to figure out all the different error scenarios just combinations of error.getMessage(), error.toString() and error.getCause()

    Toast.makeText(getApplicationContext(), "Status code :"+statusCode +"errmsg : "+error.getMessage(), Toast.LENGTH_LONG).show(); Currently you are displaying custom message ,First check which error status code and error content,There are many status codes you handle only two cases