Android response timeout

Hi my application requesting some data from the server, the application shows a Dialog to the user until the application receives the result, but in some cases the application does not receive any response and no timeout occurs and the dialog stays forever on the screen.

Below is the code for the HttpClient.

    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);  
    HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);  
    HttpProtocolParams.setUseExpectContinue(params, true);  
     // Turn off stale checking. Our connections break all the time anyway,
    // and it not worth it to pay the penalty of checking every time.
    HttpConnectionParams.setStaleCheckingEnabled(params, false);

    // Default connection and socket timeout of 30 seconds. Tweak to taste.
    HttpConnectionParams.setConnectionTimeout(params, 5*1000);
    HttpConnectionParams.setSoTimeout(params, 30*1000);
    HttpConnectionParams.setSocketBufferSize(params, 8192);

    ConnManagerParams.setTimeout(params, 5 * 1000);
    ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(50));
    ConnManagerParams.setMaxTotalConnections(params, 200);

    // Sets up the http part of the service.
    final SchemeRegistry supportedSchemes = new SchemeRegistry();

    // Register the "http" protocol scheme, it is required
    // by the default operator to look up socket factories.
    final SocketFactory sf = PlainSocketFactory.getSocketFactory();
    supportedSchemes.register(new Scheme("http", sf, 80));
    supportedSchemes.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));  
    final ThreadSafeClientConnManager ccm = new ThreadSafeClientConnManager(params,

    DefaultHttpClient httpClient = new DefaultHttpClient(ccm, params);`


please indicate an error or some other code suggestions if something is wrong.


source to share

3 answers


when the dialog box appears, it is necessary to send a delay message to the handler, when receiving a delay message, cancel the dialog and cancel the request



to cancel the request

delay time, which you can think of as a timeout or whatever you think is the longest.



It looks like you are making it too hard to get TimeOut. Try it like below:

//TimeOut is set to 20 seconds
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 20000);
HttpConnectionParams.setSoTimeout(httpParams, 20000);
HttpClient client = new DefaultHttpClient(httpParams);




I faced the same problem in one of my projects.

HttpConnectionParams.setConnectionTimeout(httpParams, 20000);
HttpConnectionParams.setSoTimeout(httpParams, 20000);


but I don't know why the above code plug didn't work in random situations. not in 20 seconds in my case.

So, I came up with a little hacky approach. I know this is not recommended. But you may find it useful. you will have 3 scenarios where you dismiss the dialogue. and move on.

1) cancel the dialogue as soon as you get a response.

2) cancel the dialog if timeout occurs

3) Write logic for dismissing dialog in dealyed. Make sure you always mention latency as what you specified in setSoTimeout

(not recommended but useful)

So, in the worst case, if you didn't receive a response / timeout within the specified timeout duration. the third approach will always work.

Hope for this help.



All Articles