ronak.mohan
ronak.mohan

Reputation: 33

Android Response Timeout

Hi my application requesting some data from server , application shows Dialog to the user till application receives result but in some cases application doesn't receive any response and no timeout occurs and dialog remains forever on the screen.

Following below is code for 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's 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,
            supportedSchemes);

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

please specify the error or some other code suggestions if something is wrong in it.

Upvotes: 3

Views: 1248

Answers (3)

Vipul
Vipul

Reputation: 28093

I faced same issue in one of my project.

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

but i dont know why above code snipper was not working on random occasions.Timeout used to happen but not in 20 seconds in my case.

So i came up with little hacky approach.I know its not recommended.but you may find it useful. you will have 3 scenarios where you will dismiss dialog.and move on.

1) dismiss dialog as soon as you receive response.

2) dismiss dialog if timeout occurs

3) Write a logic for dismissing the dialog in handler.post dealyed.make sure you always mention delay as what you have given in setSoTimeout (Not recommended but useful)

So in worst case if you dont receive response/timeout in mentioned timeout duration. third approach will always work.

Hope this help.

Upvotes: 0

idiottiger
idiottiger

Reputation: 5177

advice:

when the dialog show, need send delay message to handler, when receive the delay message, dismiss the dialog and cancel request

Hanlder.sendEmptyMessageDelayed(what,delay_time)

HttpUriRequest.abort to cancel request

the delay time you can assume the timeout or whatever you think the longest time.

Upvotes: 1

waqaslam
waqaslam

Reputation: 68187

Seems like you are making it too complicated to get TimeOut done. Try doing it as below:

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

Upvotes: 1

Related Questions