0

我一直在开发一个基于 Web 的 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();

有什么想法我该怎么办?

4

1 回答 1

1

我发现问题来源是使用 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();

希望对某人有所帮助。

于 2012-07-23T20:28:45.330 回答