Android从url获取图像SingleClientConnManager问题
我正在使用以下代码来获取徽标的图像并将其保存在数据库中。
DefaultHttpClient mHttpClient = new DefaultHttpClient();
HttpGet mHttpGet;
HttpResponse mHttpResponse;
HttpEntity entity;
for (int reportsCount = 0; reportsCount < reportsArr.length; reportsCount++) {
//Make a request to get our image
mHttpGet = new HttpGet(reportsArr[reportsCount][1]);
byte[] categoryLogoArr = null;
try {
mHttpResponse = mHttpClient.execute(mHttpGet);
if (mHttpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
entity = mHttpResponse.getEntity();
logoArr= EntityUtils.toByteArray(entity);
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long categoryID = dataHelper.addCategory(reportsArr[reportsCount][0], categoryLogoArr);
}
第一张图像添加完美,但在其余情况下它不起作用并给出以下警告。
WARN/SingleClientConnManager(2389): Invalid use of SingleClientConnManager: connection still allocated.
我的代码有什么问题?需要改变什么才能解决呢?
I am using following code to get the images of a logo and save it in the database.
DefaultHttpClient mHttpClient = new DefaultHttpClient();
HttpGet mHttpGet;
HttpResponse mHttpResponse;
HttpEntity entity;
for (int reportsCount = 0; reportsCount < reportsArr.length; reportsCount++) {
//Make a request to get our image
mHttpGet = new HttpGet(reportsArr[reportsCount][1]);
byte[] categoryLogoArr = null;
try {
mHttpResponse = mHttpClient.execute(mHttpGet);
if (mHttpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
entity = mHttpResponse.getEntity();
logoArr= EntityUtils.toByteArray(entity);
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long categoryID = dataHelper.addCategory(reportsArr[reportsCount][0], categoryLogoArr);
}
The first image added perfectly, but rest of the cases it is not working and giving the following warning.
WARN/SingleClientConnManager(2389): Invalid use of SingleClientConnManager: connection still allocated.
What is problem in my code? What to change to solve it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要先使用内容,然后才能重用连接。这可能与 使用 HttpRequest.execute() 的异常重复: Invalid use of SingleClientConnManager: connection still allocate
我认为即使响应代码不是 HttpStatus.SC_OK,并且在异常中,您也需要使用内容处理程序。
You need to consume the content before you can reuse the connection. This may be a duplicate of Exception using HttpRequest.execute(): Invalid use of SingleClientConnManager: connection still allocated
I think that you need to consume the content even if the response code is not HttpStatus.SC_OK, and in your exception handlers.