Android:AndroidHttpClient – 如何设置超时?

我已经按照kuester2000的答案的指示,但我的超时设置似乎并没有工作。

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(); } //... 

但是,超时值不会更改任何内容。

在我联系的答案中,它也说:

连接超时会抛出“java.net.SocketTimeoutException:套接字未连接”,套接字超时“java.net.SocketTimeoutException:操作超时”。

但我没有得到。 相反,我得到“org.apache.http.conn.ConnectTimeoutException:连接到…超时”

所以谁能帮助我? 错误在哪里?

Solutions Collecting From Web of "Android:AndroidHttpClient – 如何设置超时?"

您不使用httpParams参数,它们必须提供给HTTPClient。 所以它不会像这样工作。 在你的链接中,顺序是正确的! 尝试以下顺序:首先创buildParams并将它们提供给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); 

确实想把附件附加到我的http请求上,但在我的例子中正确的方法是

 httpGet.setParams(httpParams); 

在调用httpClient.execute(httpGet)之前。

刚刚添加该行,它工作正常。

另一个在客户端上设置的选项:

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

这应该导致这些特定的参数被设置…

HTH

  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(); } 

阅读后,下面是我如何使用从默认客户端直接参数:

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

原来的功劳去http://www.jayway.com/2009/03/17/configuring-timeout-with-apache-httpclient-40/