Android get image from url SingleClientConnManager problem

耗尽温柔 提交于 2019-12-23 04:07:27

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/5959751/android-get-image-from-url-singleclientconnmanager-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!