Sotimeout not working in multi-page HTTP message on Android 2.1

I am using apache httpclient, which comes with sdk for Android, to upload the file to the server using a multi-page HTTP message. The problem is that when I turn off the wifi connection on my device and the device does not have Internet access and events after setting the sotimeout timeout and connection time, the code hangs in the httpclient.execute() statement indefinitely, and this happens every time.

my code is:

 HttpClient httpclient = new DefaultHttpClient(); HttpConnectionParams.setSoTimeout(httpclient.getParams(), 5000); ConnManagerParams.setTimeout( httpclient.getParams(), 5000 ); HttpConnectionParams.setSocketBufferSize(httpclient.getParams(), 8192); HttpPost("http://myurl"); File file = new File(fileAbsolutePath); MultipartEntity mpEntity = new MultipartEntity(); ContentBody cbFile = new FileBody(file); mpEntity.addPart("uploadedfile", cbFile); httppost.setEntity(mpEntity); if(!backupCancel) { System.out.println("<<<<<<<<<<<<<<<<<<<<<<Actually transferring file>>>>>>>>>>>>>>>"); HttpResponse response = httpclient.execute(httppost); } 
+2
source share
2 answers

Try the following:

Assuming u has an httpClient object that is an instance of AndroidHttpClient or DefaultHttpClient

 HttpParams httpParams = httpClient.getParams(); httpParams.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, Timeout in milli seconds); 

The httpParams you get is a reference object, so doing setIntParameter fixes the problem.

If you really have a special requirement for custom timeouts that prefer to use AndroidHttpClient, this is very useful and solves most of our problems :) good luck

+3
source

I think you should use

 HttpParams lHttpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(lHttpParams, 3000); HttpConnectionParams.setSoTimeout(lHttpParams, 3000); 

Best wishes,
~ Anup

0
source

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


All Articles