Android的无效使用SingleClientConnManager的:连接仍然分配 [英] Android Invalid use of SingleClientConnManager: connection still allocated
本文介绍了Android的无效使用SingleClientConnManager的:连接仍然分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请注意这个问题是我的HTTP调用独一无二的。
我环顾四周,一些PPL说 HTT presponse.getEntity()consumeContent();
但我可以用它,因为我需要提供返回的数据。
我打电话给我的API doInBackground ....为返回api.post(分析,则params);
如何解决无效SingleClientConnManager到这个HTTP POST:
公共字符串后(字符串URL,列表和LT;&的NameValuePair GT; PARAMS){
HttpPost要求=新HttpPost(BASEURL +网址);
HTT presponse响应; 尝试{
request.setEntity(新UrlEn codedFormEntity(参数,可以UTF-8));
响应= client.execute(请求);
HttpEntity实体= response.getEntity(); 返回EntityUtils.toString(实体);
}
赶上(ClientProtocolException E){
返回e.toString();
}
赶上(IOException异常五){
返回e.toString();
}
} //结束后
这是我收到的错误:
12月3日至12日:33:46.926:V /活动(28803):YourAsyncTask.class
12月3日至12日:33:46.926:W /活动(28803):---------------------------------- ---------------
12月3日至12日:33:46.926:W / SingleClientConnManager(28803):使用无效的SingleClientConnManager:连接仍然分配。
12月3日至12日:33:46.926:W / SingleClientConnManager(28803):确保释放分配另一个之前的连接。
12月3日至12日:33:46.926:我/的System.out(28803):AsyncTask的#4调用detatch()
12月3日至12日:33:46.926:D /吐司(28803):checkMirrorLinkEnabled回报:假的
12月3日至12日:33:46.926:D /吐司(28803):允许展示
12月3日至12日:33:46.926:W / AsyncTaskExecutor(28803):java.util.concurrent.ThreadPoolExecutor$Worker@433abe80 [状态= -1,空队列] | AsyncTask的#5
解决方案
搜索,的我发现这个解决方案
的Htt presponse HTT presponse = httpClient.execute(httpPost);//而不是HttpClient的使用getThreadSafeClient()方法。
。HTT presponse HTT presponse = getThreadSafeClient()执行(httpPost);公共静态DefaultHttpClient getThreadSafeClient(){
DefaultHttpClient客户端=新DefaultHttpClient();
ClientConnectionManager经理= client.getConnectionManager();
的HttpParams PARAMS = client.getParams();
客户端=新DefaultHttpClient(新ThreadSafeClientConnManager(参数,可以
mgr.getSchemeRegistry()),则params);返回客户端;
}
Please note this question is unique for my HTTP call.
I have look around and some ppl say httpResponse.getEntity().consumeContent();
but I can use it, because I need to provide a return data.
I call my API doInBackground.... as return api.post("analytics", params);
How can I fix the invalid SingleClientConnManager into this http post :
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
This is the error I am getting :
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
解决方案
After searching, I found this solution
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;
}
这篇关于Android的无效使用SingleClientConnManager的:连接仍然分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文