5

I have this very big bug in my application that I really can't seem to solve. Whenever I make a rest call via 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 the 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 perfect and I receive my data as it should. I also tested the rest server call via a normal webbrowser on my computer and that always gives back my data within 100ms. So what am I doing wrong? I also tested it on another device, but that gives me the same problem. I would be SO glad if somebody could solve my problem :)

Volo
  • 28,673
  • 12
  • 97
  • 125
Martijn538
  • 51
  • 1
  • 1
  • 4
  • This probably has to do with a slow network (are you using Wi-Fi or 3G?). Try running it in the emulator. You may need to increase the connection timeout and set up a retry mechanism. – kgiannakakis Jul 15 '11 at 09:46
  • Yeah but with my other apps I can get the requested data all the time. And with one of the phones I am using WiFi, but that one gives me exceptions either. And as far as I know, The httpclient has an automatic retry mechanism? – Martijn538 Jul 15 '11 at 09:52
  • Increase the timeout to 20-30 seconds. At times, the line may be busy and hence the timeout. – Andrew Anderson Jul 15 '11 at 09:53
  • Alright, I increased the timeout to 20 seconds: HttpConnectionParams.setConnectionTimeout(httpParameters, 20000); HttpConnectionParams.setSoTimeout(httpParameters, 20000); But I still have the same situation, the data gets loaded very rarely and mostly it doesn't receive anything – Martijn538 Jul 15 '11 at 09:59
  • Hey Martijn538 Did you find the solution for your issue with timeouts? – goodm Mar 16 '12 at 11:40

4 Answers4

5

The problem is the DefaultHttpClient. Are you using it asynchronously? Since the DefaultHttpClient is not thread-safe, using it in an asynchronous environment might cause a problem. I've had this problem before when my activity started multiple Http connection at the same time and i ended up changing it to use HttpURLConnection. You can refer to this site: http://www.vogella.de/articles/AndroidNetworking/article.html

EdChum
  • 376,765
  • 198
  • 813
  • 562
1

I having the same issue.

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

Dunno if it's relevant or not, i'm still trying to find a workaround. Maybe someone here can take a look and figure it out.

ehanoc
  • 2,187
  • 18
  • 23
  • Had the exact same issue after I changed my hosts-file. A reboot of the phone solved the problem. – Niels Apr 03 '12 at 10:21
0

Had a similar issue, could be reproduced only when trying to connect using several devices simultaneously and the solution was:

Instance reboot solved problem with connections. (or just need to restart networking service)

(c) My server/instance admin

Also, there are people who experience similar issues and some other solutions worked for them, for instance:

  1. Disable tcp_timestamp
  2. Disable tcp_tw_reuse & tcp_tw_recycle
  3. Enabled vm save mode in Android manifest: android:vmSafeMode="true"

Community
  • 1
  • 1
GregoryK
  • 3,011
  • 1
  • 27
  • 26
0

Check if you are able to connect using your development machine, Also check the load times. There is a very good chance your timeouts are way too small.

Ravi Vyas
  • 12,212
  • 6
  • 31
  • 47