Android无效使用SingleClientConnManager:仍然分配了连接
请注意,此问题对于我的HTTP呼叫是唯一的。
我环顾四周,有些人说httpResponse.getEntity().consumeContent();
但我可以使用它,因为我需要提供返回数据。
我把我的API doInBackground ….称为return api.post("analytics", params);
如何将无效的SingleClientConnManager修复到此httppost中:
public String post(String url, List 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
搜索后, 我找到了这个解决方案
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; }
- 对于具有GSON的字段,没有特定结构的Parse JSON
- 如何使用Retrofit和RxJava / RxAndroid处理响应错误?
- 使用PHP openssl_verify()函数来validationAndroid客户端APP创建的签名和数据
- 在ASyncTask.execute()之后从Activity执行x()
- 如何从ISO 8601格式字符串中选择时区到日历实例中
- Android:如何将片段加载到FrameLayout中
- 简单的AlarmManager示例,用于在10分钟内触发活动
- 选择状态后,使用notifyDataSetChanged更新City Spinner
- Android在广播接收器中捕获音量增/减按键?