HttpURLConnection下载不了图片,哪里配置错了?

mimixi666 发布于 2014/10/10 08:30
阅读 1K+
收藏 1

为啥httpurlconnection下载不了,而httpclient却能下载?

哪里配置错误了?

httpurlconnection:

private Bitmap downloadBitmapUseHttpURLConnection(String imageUrl) {
                Bitmap bitmap = null;
                HttpURLConnection con = null;
                try {
                        URL url = new URL(imageUrl);
                        con = (HttpURLConnection) url.openConnection();
                        con.setConnectTimeout(5 * 1000);
                        con.setReadTimeout(10 * 1000);
                        con.setDoInput(true);
                        con.setDoOutput(true);
                        bitmap = BitmapFactory.decodeStream(con.getInputStream());
                } catch (Exception e) {
                        e.printStackTrace();
                } finally {
                        if (con != null) {
                                con.disconnect();
                        }
                }
                return bitmap;
        }

httpclient:

private Bitmap downloadBitmapUseHttpClient(String imageUrl) {
                Bitmap bitmap = null;
                DefaultHttpClient httpclient = new DefaultHttpClient();
                HttpGet httpget = new HttpGet(imageUrl);
                InputStream in = null;
                try {
                        HttpResponse resp = httpclient.execute(httpget);
                        // 判断是否正确执行
                        if (HttpStatus.SC_OK == resp.getStatusLine().getStatusCode()) {
                                // 将返回内容转换为bitmap
                                HttpEntity entity = resp.getEntity();
                                in = entity.getContent();
                                bitmap = BitmapFactory.decodeStream(in);
                                in.close();
                        }

                } catch (Exception e) {
                        e.printStackTrace();
                } finally {
                        httpclient.getConnectionManager().shutdown();
                }

                return bitmap;
        }

 

哪里配置有问题?

加载中
1
Steve
Steve

楼主,apache commons有个很优雅的方法,一句搞定:   
public static void copyURLToFile(URL source, File destination) throws IOException {}

如果你是maven,引入如下依赖就ok:
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.4</version>
</dependency>

简单安全无痛。


0
iuKa
iuKa

不知道是不是这个原因:

http://stackoverflow.com/questions/10650660/android-bitmapfactory-decodestream-returns-null

最好也要把错误日志贴出来

PS.图不错

0
码农明明
码农明明

我用着都没问题啊

try {
            URL url = new URL(mUrl);
            URLConnection urlConnection = url.openConnection();
            int contentLenght = urlConnection.getContentLength();
            InputStream is = urlConnection.getInputStream();
            byte[] bs = new byte[1024];
            int len;
            int nowlength = 0;
            OutputStream os = new FileOutputStream(file);
            while((len = is.read(bs)) != -1) {
                os.write(bs, 0, len);
                nowlength = nowlength + len;
                publishProgress(nowlength*100/contentLenght);
            }
            os.close();
            is.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }




0
Ryan-瑞恩
Ryan-瑞恩
手段不行!
0
realanan
realanan

你没有检查StatusCode,如果不是200的话就下不到数据。
可以先用浏览器F12的功能看下网络通信的过程,有些图片网站可能会用一个302或307跳转,这个时候用HttpURLConnection的话需要自己写跳转处理。

0
pseudo
pseudo
把  con.setDoOutput(true);注释了



0
mimixi666
mimixi666

引用来自“iuKa”的评论

不知道是不是这个原因:

http://stackoverflow.com/questions/10650660/android-bitmapfactory-decodestream-returns-null

最好也要把错误日志贴出来

PS.图不错

我发觉的是,它是有时候行得通,有时候行不通,就我本来的那个程序,不知道怎么回事。。。

乐乐呵
我觉着是流的问题
0
zhx2012
zhx2012

有什么异常?   返回码是是多少? 


返回顶部
顶部