Getting Java.Net.Sockettimeoutexception: Connection Timed Out in Android

Getting java.net.SocketTimeoutException: Connection timed out in android

I've searched all over the web and after reading lot of docs regarding connection timeout exception, the thing I understood is that, preventing SocketTimeoutException is beyond our limit. One way to effectively handle it is to define a connection timeout and later handle it by using a try-catch block. Hope this will help anyone in future who are facing the same issue.

HttpUrlConnection conn = (HttpURLConnection) url.openConnection();

//set the timeout in milliseconds
conn.setConnectTimeout(7000);

Android java.net.SocketTimeoutException: Connection timed out

There are two possibilities,

1)have you checked and tested your connection.

2)better don't set any connection timeout,if you are setting chose maximum time,,bcos it throws an error,if server didn't response within given time..

java.net.socketexception: The operation timed out problem in android?

This is happening because some times your server is taking too long to respond. Actually this could also happening due to a slow network, so you don't have full control over it. If you were using HttpClient, you could increase the timeout period:

HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpParameters, WAIT_RESPONSE_TIMEOUT);
HttpConnectionParams.setTcpNoDelay(httpParameters, true);

client = new DefaultHttpClient(httpParameters);

CONNECTION_TIMEOUT is the time to wait for a connection to establish. WAIT_RESPONSE_TIMEOUT is the time to wait for data to be received - in your case this is what you need to increase.

Bottom line:

  • Stop using URL and start using HttpClient now.
  • The situation you are describing is a very common one in a production application and you need to cater for it. Increasing the timeout won't always help, as your application will appear to halt when there is a slow network. You could add a retry mechanism or inform the user that the request has failed and ask him to try again.


Related Topics



Leave a reply



Submit