Android HttpEntry读取挂在Droid x上

时间:2012-07-15 16:18:37

标签: java android http inputstream

我一直在研究一个基于网络的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();

任何想法我应该怎么做?

1 个答案:

答案 0 :(得分: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();

希望能有所帮助。