Apache HttpClient 4.0 does not work with socket timeout on Android

I am working on an Android application that requires using HttpClient to upload a file from an Android device to a web server. The download file can be up to 1 GB in size, so timeouts can occur if the device loses connection during the download. The strange thing is that the timeout that I set for the socket does not seem to have any effect. The application will just freeze when I lose the connection, instead of raising a SocketTimeoutException.

I tried using:

HttpConnectionParams.setConnectionTimeout(params, CrashLogParams.TIMEOUT); HttpConnectionParams.setSoTimeout(params, CrashLogParams.TIMEOUT); 

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

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

The reason I know that the connection timeout is due to the fact that I get an exception for the connection timeout after execution

 httpclient.execute(httppost); 

An application appears to hang when the connection is lost during the download, but after the application has successfully connected to the server.

To test my application, I disconnected the network at different times to see how the application would react. If I disconnect the network before sending the request, I get a connection error, and my application can gracefully process it, but if I disconnect it while the application is loading, it freezes. Of course, I am doing all these requests through AsyncTasks, so the main UI thread is not crashing. I am just wondering if there is another way to make sure the socket will time out without receiving any data, or if I missed something here. I read a lot of blogs and posts, but most of them just suggest using SO_TIMEOUT, which doesn't work for me.

sotimeout does not work in multi-page HTTP message on Android 2.1

+6
source share
2 answers

Do you create your own ClientConnectionManager? Take a look at AndroidHttpClient.newInstance () ,

They create a new BasicHttpParams object and pass it to the constructors for both ThreadSafeClientConnectionManager and DefaultHttpClient. I do not see a way to set parameters in the ClientConnectionManager, except for the constructor.

0
source

I ran into the same issue as you, with the same use case. This happens on the samsung s2 galaxy running Android 2.4.6, but not with the same 4.x device. Unfortunately, this is exactly the device that my client uses, and it works fine on about 10 other test devices with different versions of Android and constructors ...

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

The only workaround I found was to use the HttpClient.execute() method in a separate thread and calling thread.join(timeout) as protection to stop the thread if something goes wrong. The disadvantage is that loading is performed normally, but takes longer than interruption, the request is interrupted ...

If you could find something, I would really appreciate it if you could share it.

0
source

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


All Articles