Android get image from url SingleClientConnManager

2019-06-02 02:38发布

问题:

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.