How to handle HTTP timeout?
22,518
Solution 1
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);
}
Solution 2
Increase your time of waiting for response like :
HttpConnectionParams.setConnectionTimeout( httpParameters, 60000 ); //1 minute
HttpConnectionParams.setSoTimeout( httpParameters, 90000 ); // 1.5 minute
Author by
Louis Evans
Updated on August 06, 2020Comments
-
Louis Evans over 3 years
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?
-
user370305 over 10 yearsSimple put
try-catch
block and catch the TimeOut. -
Boris Mocialov over 10 yearsThe connection timeout throws "java.net.SocketTimeoutException: Socket is not connected" and the socket timeout "java.net.SocketTimeoutException: The operation timed out". so try catch
-
Hossam Oukli over 10 yearsYou need to accept VM's Answer it was his idea.
-
-
VM4 over 10 yearsThat's because SocketTimeoutException is a SubType of IOException. :)
-
VM4 over 10 yearsCorrected it to catch both.
-
Amit almost 3 yearsgotta catch'em all