Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Http connection timeout on Android not working

I'm writing an application that connects to a webservice and I don't want it to wait too long if it can't get a connection. I therefore set the connectionTimeout of the httpparams. But it doesn't seem to have any effect whatsoever.

To test I turn of my WLAN temporarily. The application tries to connect for quite some time (way more than the 3 seconds I want) and then throws an UnknownHostException.

Here is my code:

try{     HttpClient httpclient = new DefaultHttpClient();     HttpParams params = httpclient.getParams();     HttpConnectionParams.setConnectionTimeout(params, 3000);     HttpConnectionParams.setSoTimeout(params, 3000);      httppost = new HttpPost(URL);     StringEntity se = new StringEntity(envelope,HTTP.UTF_8);     httppost.setEntity(se);     //Code stops here until UnknownHostException is thrown.     BasicHttpResponse httpResponse = (BasicHttpResponse) httpclient.execute(httppost);      HttpEntity entity = httpResponse.getEntity();     return entity;  }catch (Exception e){     e.printStackTrace(); } 

Anyone have any ideas what I missed?

like image 654
ascu Avatar asked Jun 19 '10 12:06

ascu


People also ask

How do I fix connection timeout on Android?

Restart your device. Open your Settings app and tap Network & internet or Connections. Depending on your device, these options may be different. Turn Wi-Fi off and mobile data on, and check if there's a difference. If not, turn mobile data off and Wi-Fi on and check again.

What causes HTTP connection timeout?

If the server takes so long to respond, a timeout error displays. This error is meant to prevent devices from waiting ceaselessly for the server to respond. The possible causes may be a server issue, outdated browser and cache, blacklisted sites, sporadic internet connection, faulty extensions, etc.

What does it mean when it says connection timeout?

A server connection timeout means that a server is taking too long to reply to a data request made from another device.


1 Answers

Try to do it this way:

HttpPost httpPost = new HttpPost(url); StringEntity se = new StringEntity(envelope,HTTP.UTF_8); httpPost.setEntity(se);  HttpParams httpParameters = new BasicHttpParams(); // Set the timeout in milliseconds until a connection is established. int timeoutConnection = 3000; HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); // Set the default socket timeout (SO_TIMEOUT)  // in milliseconds which is the timeout for waiting for data. int timeoutSocket = 3000; HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);  DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters); BasicHttpResponse httpResponse = (BasicHttpResponse)  httpClient.execute(httpPost);  HttpEntity entity = httpResponse.getEntity(); return entity; 

You then can catch a possible ConnectTimeoutException.

like image 130
Cristian Avatar answered Oct 01 '22 05:10

Cristian