2011-05-11 64 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