请注意,此问题对于我的HTTP通话是唯一的。
我环顾四周,有些人说httpResponse.getEntity().consumeContent();
但我可以使用它,因为我需要提供返回数据。
我将我的API doInBackground ....称为return api.post("analytics", params);
如何将无效的SingleClientConnManager修复到此http帖子中:
public String post(String url, List<NameValuePair> params) {
HttpPost request = new HttpPost(BASEURL + url);
HttpResponse response;
try {
request.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
response = client.execute(request);
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity);
}
catch(ClientProtocolException e) {
return e.toString();
}
catch(IOException e) {
return e.toString();
}
}//end post
这是我得到的错误:
03-12 12:33:46.926: V/Activity(28803): YourAsyncTask.class
03-12 12:33:46.926: W/Activity(28803): -------------------------------------------------
03-12 12:33:46.926: W/SingleClientConnManager(28803): Invalid use of SingleClientConnManager: connection still allocated.
03-12 12:33:46.926: W/SingleClientConnManager(28803): Make sure to release the connection before allocating another one.
03-12 12:33:46.926: I/System.out(28803): AsyncTask #4 calls detatch()
03-12 12:33:46.926: D/Toast(28803): checkMirrorLinkEnabled returns : false
03-12 12:33:46.926: D/Toast(28803): showing allowed
03-12 12:33:46.926: W/AsyncTaskExecutor(28803): java.util.concurrent.ThreadPoolExecutor$Worker@433abe80[State = -1, empty queue] | AsyncTask # 5
答案 0 :(得分:1)
HttpResponse httpResponse = httpClient.execute(httpPost);
//instead of httpClient use getThreadSafeClient() method.
HttpResponse httpResponse = getThreadSafeClient().execute(httpPost);
public static DefaultHttpClient getThreadSafeClient() {
DefaultHttpClient client = new DefaultHttpClient();
ClientConnectionManager mgr = client.getConnectionManager();
HttpParams params = client.getParams();
client = new DefaultHttpClient(new ThreadSafeClientConnManager(params,
mgr.getSchemeRegistry()), params);
return client;
}