Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
哪种http方法在android中最好?_Android_Multithreading_Http_Connection - Fatal编程技术网

哪种http方法在android中最好?

哪种http方法在android中最好?,android,multithreading,http,connection,Android,Multithreading,Http,Connection,我有两种方法从互联网下载图片,结果是一个inputstream。 但其中一个会使下载图像失败,我不知道为什么, 下面是有bug的代码: HttpGet get = new HttpGet(imageName); HttpResponse response = (HttpResponse) httpClient.execute(get); HttpEntity entity = response.getEntity();

我有两种方法从互联网下载图片,结果是一个inputstream。 但其中一个会使下载图像失败,我不知道为什么, 下面是有bug的代码:

            HttpGet get = new HttpGet(imageName);
            HttpResponse response = (HttpResponse) httpClient.execute(get);
            HttpEntity entity = response.getEntity();
            BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity); 
            InputStream is = bufHttpEntity.getContent();
这是我正在使用的另一个:

        URL imageUrl = new URL(imageName);
        conn = (HttpURLConnection) imageUrl
                .openConnection();
        conn.setConnectTimeout(30000);
        conn.setReadTimeout(30000);
        conn.setInstanceFollowRedirects(true);
        InputStream is = conn.getInputStream();
我想知道是否有人能告诉我 1为什么我使用方法1来获取无法显示图片的inputstram 2我看到有人说,使用httpclient,不要使用连接。我不知道原因??连接是否比http客户端更差


我在多线程环境中使用,方法2正常工作,但方法1不能。

使用bufferedinputstream更好??
public static Bitmap getimage(String imageUrl) {

        Log.i("imageurl", imageUrl);
        Bitmap bitmap = null;
        BufferedInputStream bis = null;
        InputStream is = null;

        try {
            String url = imageUrl;
            URL myUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) myUrl.openConnection();

            conn.setDoInput(true);
            conn.connect();
            if (conn.getResponseCode() != 404) {
                is = conn.getInputStream();
                bis = new BufferedInputStream(is, 8192);
                bitmap = BitmapFactory.decodeStream(bis);

            }
            conn.disconnect();

        }catch (Exception e) {
             Log.e("getImageData", e.toString());
             return bitmap;
        }
        finally{
            if (bis != null) 
            {
                try{
                    bis.close();
                }catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (is != null) 
            {
                try {
                    is.close();
                }catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
         return bitmap;
    }