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

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

问题描述

我遵循的<一个指令href="http://stackoverflow.com/questions/693997/how-to-set-htt$p$psponse-timeout-for-android-in-java">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.

这篇关于安卓:AndroidHttpClient - 如何设置超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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