gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:11:31 30 4
gpt4 key购买 nike

我已按照 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。所以它不会像这样工作。在您链接的答案中,顺序是正确的!尝试以下顺序:首先创建参数并将它们提供给 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 - 如何设置超时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5730078/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com