Android:AndroidHttpClient - 如何设置超时? [英] Android: AndroidHttpClient - how to set timeout?
问题描述
我已按照 kuester2000 的回答,但我的超时设置似乎不起作用.
I have followed the instructions of kuester2000's answer, but my timeout settings don't 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 doesn't change anything.
在我链接的答案中,它还说:
In the answer I linked, it also says:
连接超时抛出java.net.SocketTimeoutException: Socket is not connected"和套接字超时java.net.SocketTimeoutException: The operation timed out".
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:连接到...超时"
But I get neither. Instead I get "org.apache.http.conn.ConnectTimeoutException: Connect to ... timed out"
所以有人可以帮我吗?错在哪里?
so can anybody help me? where is the mistake?
推荐答案
我确实错过了将参数附加到我的 http 请求,但在我的示例中执行此操作的正确方法是
I did miss to attach the params to my http request, but the proper way to do this in my example is
httpGet.setParams(httpParams);
在调用 httpClient.execute(httpGet) 之前.
before calling httpClient.execute(httpGet).
刚刚添加了该行,效果很好.
Just added that line and it worked fine.
这篇关于Android:AndroidHttpClient - 如何设置超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!