Android从url获取图像SingleClientConnManager问题

发布于 2024-11-07 02:52:48 字数 1130 浏览 5 评论 0原文

我正在使用以下代码来获取徽标的图像并将其保存在数据库中。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羁绊已千年 2024-11-14 02:52:48

您需要先使用内容,然后才能重用连接。这可能与 使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文