gpt4 book ai didi

java - Android HttpEntry 读取在 Droid x 上挂起

转载 作者:行者123 更新时间:2023-12-01 15:16:50 26 4
gpt4 key购买 nike

我一直在开发一个基于网络的 Android 应用程序,我们希望该应用程序将数据发布到服务器并以 JSONString 形式读取响应,该代码在 3 个不同的设备上进行了测试,所有结果都正常,除了 Droid X ,它只是在从 HttpResponse 读取 inputStream 时挂起。这是我的代码:

HttpResponse response = mClient.execute(mPost);
StatusLine status_line = response.getStatusLine();
int status_code = status_line.getStatusCode();
/*
* Not ok Response
*/
if (status_code != 200){
mBusy = false;
mListener.log("Error Status code = "+String.valueOf(status_code));
return false;
}

HttpEntity entry = response.getEntity();
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
mResponse = reader.readLine();

有什么想法我应该做什么吗?

最佳答案

我发现问题根源在于使用android的默认HTTP客户端。相反,我使用了:

            HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
HttpProtocolParams.setUseExpectContinue(params, false);
HttpConnectionParams.setConnectionTimeout(params, 10000);
HttpConnectionParams.setSoTimeout(params, 10000);
ConnManagerParams.setMaxTotalConnections(params, 100);
ConnManagerParams.setTimeout(params, 30000);

SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http",PlainSocketFactory.getSocketFactory(), 80));
registry.register(new Scheme("https",PlainSocketFactory.getSocketFactory(), 80));
ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager(params, registry);
mClient = new DefaultHttpClient(manager, params);

而不是:

mClient = new DefaultHttpClient();

希望对某人有帮助。

关于java - Android HttpEntry 读取在 Droid x 上挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11493531/

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