安卓:AndroidHttpClient - 如何设置超时?如何设置、AndroidHttpClient

2023-09-13 01:29:27 作者:男硬汉@

我遵循的kuester2000's回答,但我超时设置似乎不工作。

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:套接字未连接和插座超时。java.net.SocketTimeoutException:操作超时

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?

推荐答案

我的没有的小姐到PARAMS连接到我的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.

 
精彩推荐
图片推荐