java – Android:AndroidHttpClient – 如何设置超时?

我已按照kuester2000’s answer的说明进行操作,但我的超时设置似乎不起作用.

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

但是,超时值不会改变任何内容.

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

The connection timeout throws “java.net.SocketTimeoutException: Socket is not connected” and the socket timeout “java.net.SocketTimeoutException: The operation timed out”.

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

有人能帮助我吗?哪里出错了?

最佳答案
您不使用httpParams参数,它们必须提供给HTTPClient.所以它不会像这样工作.在您链接的anwer中,订单是正确的!请尝试以下顺序:首先创建Params并将它们提供给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);

转载注明原文:java – Android:AndroidHttpClient – 如何设置超时? - 代码日志