1

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?

dev_android
  • 8,698
  • 22
  • 91
  • 148

1 Answers1

0

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.

Community
  • 1
  • 1
Mark Allison
  • 21,839
  • 8
  • 47
  • 46
  • but i failed to add catch (HttpResponseException e) { e.response.parseAsString(); }...it is giving "e.response cannot be resolved or is not a field" – dev_android May 11 '11 at 13:06
  • According to the javadocs for the Android version of HttpClient, HttpResponseException does not support a "response" member, so you should not be trying to access it. Just ensure that you call entity.consumeContent() for all possible scenarios. – Mark Allison May 11 '11 at 13:13
  • I used if (entity != null) entity.consumeContent(); mHttpClient.getConnectionManager().shutdown(); and it works perfectly. – dev_android May 12 '11 at 07:20