【问题标题】:Android get image from url SingleClientConnManager problemAndroid 从 url SingleClientConnManager 获取图像问题
【发布时间】:2011-05-11 05:39:38
【问题描述】:

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

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.

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

【问题讨论】:

    标签: android bytearray httpresponse http-error


    【解决方案1】:

    您需要先使用内容,然后才能重用连接。这可能与Exception using HttpRequest.execute(): Invalid use of SingleClientConnManager: connection still allocated 重复

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

    【讨论】:

    • 但我未能添加 catch (HttpResponseException e) { e.response.parseAsString(); }...它给出“e.response 无法解析或不是字段”
    • 根据 Android 版本的 HttpClient 的 javadocs,HttpResponseException 不支持“响应”成员,因此您不应该尝试访问它。只需确保为所有可能的情况调用 entity.consumeContent()。
    • 我用过 if (entity != null) entity.consumeContent(); mHttpClient.getConnectionManager().shutdown();而且效果很好。
    猜你喜欢
    • 1970-01-01
    • 2018-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-28
    • 2015-01-29
    相关资源
    最近更新 更多