Android: AndroidHttpClient - how to set a timeout?

I followed the instructions of kuester2000's answer , but my timeout settings do not seem to work.

try { int timeout = 3000; URL myURL = //some valid URL AndroidHttpClient = AndroidHttpClient.newInstance("name"); HttpGet httpGet = new HttpGet(myURL.toExternalForm()); HttpParams httpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams, timeout); HttpConnectionParams.setSoTimeout(httpParams, timeout); HttpResponse response = httpClient.execute(httpGet); //... } catch (SocketTimeoutException e) { e.printStackTrace(); } catch (ConnectTimeoutException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } //... 

However, the timeout value does not change anything.

In the answer I linked, he also says:

The connection timeout throws "java.net.SocketTimeoutException: Socket not connected" and the socket timeout "java.net.SocketTimeoutException: operation timeout".

But I do not get either one or the other. Instead, I get "org.apache.http.conn.ConnectTimeoutException: connection to ... timeout"

so can anyone help me? where is the mistake?

+7
source share
5 answers

I didn’t have enough to attach the parameters to my HTTP request, but the correct way to do this in my example is

 httpGet.setParams(httpParams); 

before calling httpClient.execute (httpGet).

Just added this line and it worked perfectly.

+6
source

You do not use httpParams parameters, they must be provided by HTTPClient. So this will not work. In the Andrew that you tied, the order is correct! Try the following procedure: first create Params and put them in the HTTPClient.

 HttpParams httpParameters = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParameters, 5000); HttpConnectionParams.setSoTimeout(httpParameters, 10000); HttpClient client = new DefaultHttpClient(httpParameters); HttpGet request = new HttpGet(url); HttpResponse response = client.execute(request); 
+9
source

Another installation option on the client itself:

 AndroidHttpClient client = AndroidHttpClient.newInstance("Client V/1.0"); HttpConnectionParams.setConnectionTimeout(this.client.getParams(), 3000); HttpConnectionParams.setSoTimeout(this.client.getParams(), 5000); 

This should cause these specific parameters to be set ...

NTN

+5
source
  try { HttpGet httpGet = new HttpGet("your_uri/test.json"); HttpParams httpParameters = new BasicHttpParams(); // Set the timeout in milliseconds until a connection is established. // The default value is zero, that means the timeout is not used. int timeoutConnection = 5000; HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); // Set the default socket timeout (SO_TIMEOUT) // in milliseconds which is the timeout for waiting for data. int timeoutSocket = 10000; HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); HttpClient httpclient = new DefaultHttpClient(); httpGet.setParams(httpParameters); HttpResponse response = httpclient.execute(httpGet); HttpEntity entity = response.getEntity(); BufferedReader br = null; if(entity != null) { Log.i("read", "nube"); br = new BufferedReader(new InputStreamReader(entity.getContent())); } else { Log.i("read", "local"); AssetManager am = getApplicationContext().getAssets(); InputStream is = am.open("test.json"); br = new BufferedReader(new InputStreamReader(is)); } String line; String texto = ""; while ((line = br.readLine()) != null) { texto += line; } } catch (IOException e) { e.printStackTrace(); } 
0
source

After reading here, how I did it, using options directly from the default client:

 HttpClient client = new DefaultHttpClient(); HttpParams params = client.getParams(); HttpConnectionParams.setConnectionTimeout(params, 3000); HttpConnectionParams.setSoTimeout(params, 3000); 

Original loan is sent http://www.jayway.com/2009/03/17/configuring-timeout-with-apache-httpclient-40/

0
source

All Articles