2011-05-11 115 views
1

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

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. 

我的代码有什么问题?要改变什么来解决它?

回答

0

您需要消耗内容才能重新使用连接。这可能是Exception using HttpRequest.execute(): Invalid use of SingleClientConnManager: connection still allocated

的重复我认为即使响应代码不是HttpStatus.SC_OK,并且在您的异常处理程序中,也需要消耗内容。

+0

但我无法添加赶上(HttpResponseException E){ \t \t \t \t e.response.parseAsString(); \t \t \t \t} ...它给“e.response无法解决或不是字段” – 2011-05-11 13:06:37

+0

根据Android版本的HttpClient的javadocs,HttpResponseException不支持“响应”成员,所以你应该不要试图访问它。只要确保你为所有可能的场景调用entity.consumeContent()。 – 2011-05-11 13:13:23

+0

我使用,如果(实体!= NULL) \t \t \t \t \t \t \t \t \t entity.consumeContent(); \t \t \t \t \t \t \t \t mHttpClient.getConnectionManager()shutdown()方法。它完美的工作。 – 2011-05-12 07:20:59