开发者

Apache HttpClient 4.0 failing to timeout for socket on Android

I'm working on an Android application that requires the use of HttpClient in order to upload a file from the Android device to a web server. The file I'm uploading can reach sizes up to 1 Gb, and so timeouts can occur if the device loses connection during the upload. The weird thing is that the timeout I set for the socket doesn't seem to have any effect. The application would just hang whenever I lose connection instead of raising the SocketTimeoutException.

I tried using:

HttpConnectionParams.setConnectionTimeout(params, CrashLogParams.TIMEOUT);
HttpConnectionParams.setSoTimeout开发者_Go百科(params, CrashLogParams.TIMEOUT);

but this only worked for the connection timeout, not the socket timeout. Also I tried:

HttpParams p = httpclient.getParams();
p.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 10000);
p.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);

The reason I know that connection timeout works is because, I would get the exception for connection timeout after executing

httpclient.execute(httppost);

The application seems to hang when connection is lost during upload, but after the application has successfully made a connection to the server.

To test my app, I disabled the network at different times to see how the application would react. If I disable the network before sending the request, I get a connection error and my application can gracefully handle that, but if I disable it during upload the application hangs. Of course I'm doing all these requests through AsyncTasks, so the main UI Thread doesn't crash. I'm just wondering if there is any other way to make sure that the socket will timeout upon not receiving any data, or if I'm missing anything here. I've read many blogs and posts, but most of them just suggest using SO_TIMEOUT which doesn't work for me.

sotimeout not working in a multipart http post on android 2.1


Are you creating your own ClientConnectionManager? Take a look at the source for AndroidHttpClient.newInstance().

They create a new BasicHttpParams object and pass it to the constructors for both a ThreadSafeClientConnectionManager and the DefaultHttpClient. I don't see a way to set parameters on the ClientConnectionManager except in the constructor.


I'm facing the same problem as you, with the same use case. It happens on a samsung galaxy s2 running android 2.3.6 but not with the same device on 4.x . Unfortunately this is exactly the device my customer uses, and it runs fine on roughly 10 other test devices with various Android versions and constructors...

I spent hours trying with HttpUrlConnection library instead of HttpClient from Apache, but the end result is the same. AndroidHttpClient shows the same behavior. This leads me to say that it sounds like an hardware implementation or OS related problem...

The only workaround I found was to put the HttpClient.execute() method in a separate thread and call thread.join(timeout) as a security to stop the thread if anything goes wrong. The drawback is when upload runs fine but takes longer than the timeout, the request is interrupted...

If you found something in the meantime, I would greatly appreciate if you could share it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜