Android get image from url SingleClientConnManager problem
Asked Answered
F

1

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?

Fiche answered 11/5, 2011 at 5:39 Comment(0)
A
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.

Axe answered 11/5, 2011 at 12:18 Comment(3)
but i failed to add catch (HttpResponseException e) { e.response.parseAsString(); }...it is giving "e.response cannot be resolved or is not a field"Fiche
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.Axe
I used if (entity != null) entity.consumeContent(); mHttpClient.getConnectionManager().shutdown(); and it works perfectly.Fiche

© 2022 - 2024 — McMap. All rights reserved.