Android:AndroidHttpClient - 如何设置超时? [英] Android: AndroidHttpClient - how to set timeout?

查看:22
本文介绍了Android:AndroidHttpClient - 如何设置超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已按照 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆