从服务器下载图像时出错

发布于 2024-10-25 00:18:02 字数 494 浏览 5 评论 0原文

我从 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);

I am getting this error from the logcat

org.apache.http.NoHttpResponseException: The target server failed to respond.

What might be the reason?
My download code is as below.

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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓦然回首 2024-11-01 00:18:02

调用以下方法并传入 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;

    }

call following methods and pass in URL .

public Bitmap DownloadFromUrl(String imageURL) { //this is the downloader method

        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;

    }
酒与心事 2024-11-01 00:18:02

NoHttpResponseException 意味着一件事,也是唯一的一件事:服务器在没有发回 HTTP 响应消息的情况下关闭了连接,很可能是由于在处理请求期间遇到异常情况。换句话说,这很可能是服务器端错误。

NoHttpResponseException means one and only thing: the server closed connection without sending back an HTTP response message, most likely due to an abnormal condition encountered during processing of the request. In other words, this is most likely a server side bug.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文