Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle HTTP timeout?

In my application, I am downloading JSON data from a ReST web service. Most of the time, this works fine, however sometimes the connection will time out.

This is the code I use to set the timeout...

HttpConnectionParams.setConnectionTimeout( httpParameters, 20000 );
HttpConnectionParams.setSoTimeout( httpParameters, 42000 );

If the connection times out, the application crashes and closes, how do I handle a time out?

like image 567
Louis Evans Avatar asked Jul 20 '26 18:07

Louis Evans


1 Answers

The HttpClient class throws a ConnectTimeoutException Exception, so you should listen for it:

try {
        HttpResponse response = client.execute(post);
                    // do something with response
    } catch (ConnectTimeoutException e) {
        Log.e(TAG, "Timeout", e);
    } catch (SocketTimeoutException e) {
        Log.e(TAG, " Socket timeout", e);
    }
like image 144
VM4 Avatar answered Jul 22 '26 06:07

VM4



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!