HttpClient execution continues to throw ConnectTimeoutException

I have this very big mistake in my application that I really cannot solve. Whenever I make a rest call through the following code:

HttpGet request = new HttpGet(url + getParams()); HttpParams httpParameters = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParameters, 5000); HttpConnectionParams.setSoTimeout(httpParameters, 10000); DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters); httpClient.execute(request); 

I get an error in DDMS:

 07-15 11:22:47.448: WARN/System.err(973): org.apache.http.conn.ConnectTimeoutException: Connect to (some ip-address) timed out 

But sometimes the code works fine, and I get my data as it should. I also checked the rest of the server call using a regular web browser on my computer and always return my data within 100 ms. So what am I doing wrong? I also tested it on another device, but this gives me the same problem. I would be glad if someone could solve my problem :)

+5
source share
4 answers

The problem is DefaultHttpClient . Do you use it asynchronously? Since DefaultHttpClient not thread safe, using it in an asynchronous environment can cause a problem. I used to have this problem when my activity started multiple Http connections at the same time, and I eventually changed it to use HttpURLConnection . You can refer to this site: http://www.vogella.de/articles/AndroidNetworking/article.html

+5
source

I have the same problem.

Try using a direct IP address for your requests. I noticed that Android searches sometimes behave rather strangely. I searched and found this: http://mailinglists.945824.n3.nabble.com/Android-and-reverse-DNS-lookup-issues-td3011461.html .

I don't know if this is appropriate or not, I'm still trying to find a workaround. Maybe someone here can take a look and figure it out.

+1
source

Check if you can connect using your development machine. Also check boot time. There is a very good chance that your timeouts are too small.

0
source

If a similar problem could be reproduced only when trying to connect using multiple devices at the same time, and the solution was:

Rebooting the instance resolves the connection problem. (or just need to restart the network service)

(c) My server / admin instance

In addition, there are people who face similar problems, and some other solutions work for them, for example:

  • Disable tcp_timestamp
  • Disable tcp_tw_reuse and tcp_tw_recycle
  • Enable vm save mode in Android manifests: android: vmSafeMode = "true"

0
source

Source: https://habr.com/ru/post/985587/


All Articles