gpt4 book ai didi

android - Android 4.0 及更高版本的 httpclient.execute(httppost) 问题

转载 作者:行者123 更新时间:2023-11-29 16:11:08 24 4
gpt4 key购买 nike

我正在为 Android 编写应用程序。该应用程序连接到服务器以检索评论。感兴趣的代码是:

BufferedReader input = null;
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.domain.com/personal_proj/directorydirectory/file.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
Log.i("TEST","after httpclient.execute()");
HttpEntity entity = response.getEntity();

我在 HTC Wildfire、HTC Desire Z 中测试了这段代码,它可以正常工作。如果此代码在 Android 4.0+ 中执行,它永远不会到达该行

Log.i("TEST","after httpclient.execute()");

,服务器永远不会收到请求,也永远不会抛出异常。

有什么想法吗?

最佳答案

我不确定无参数构造的 DefaultHttpClient 的默认连接和套接字超时参数是什么,但我会检查一下,或设置参数:

HttpParams 参数 = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(参数, 15000);
HttpConnectionParams.setSoTimeout(参数, 20000);

然后使用参数构造您的 HttpClient:

新的 DefaultHttpClient(参数)

到那时,如果没有其他响应,您至少应该得到超时。

另请参阅 AndroidHttpClient (它设置了合理的超时,以及 gzip 支持等),并考虑对 prefer HttpUrlConnection 的建议(尽管有一些警告)。

关于android - Android 4.0 及更高版本的 httpclient.execute(httppost) 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13141153/

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