从服务器下载图像时出错

时间:2011-03-19 05:10:30

标签: android httpclient

我从logcat

收到此错误
  

org.apache.http.NoHttpResponseException:目标服务器无法响应。

可能是什么原因?
我的下载代码如下。

HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse)httpclient.execute(httpRequest);

HttpEntity entity = response.getEntity();
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
InputStream instream = bufHttpEntity.getContent();
bmImg = BitmapFactory.decodeStream(instream);

2 个答案:

答案 0 :(得分:1)

调用以下方法并传入URL。

public Bitmap DownloadFromUrl(String imageURL){//这是下载方法

        Bitmap bm=null;
            try {
                    URL url = new URL(imageURL); //you can write here any link



                   URLConnection ucon = url.openConnection();


                  InputStream is = ucon.getInputStream();
                   BufferedInputStream bis = new BufferedInputStream(is);


                    ByteArrayBuffer baf = new ByteArrayBuffer(50);
                   int current = 0;
                   while ((current = bis.read()) != -1) {
                            baf.append((byte) current);
                   }

                  bm= BitmapFactory.decodeByteArray(baf.toByteArray(), 0, baf.toByteArray().length);


          } catch (IOException e) {
                    Log.d("ImageManager", "Error: " + e);
          }
            return bm;

    }

答案 1 :(得分:1)

NoHttpResponseException表示唯一的事情:服务器关闭连接而不发回HTTP响应消息,很可能是由于在处理请求期间遇到异常情况。换句话说,这很可能是服务器方面的错误。

相关问题