毕加索没有加载图像

Picasso not loading image

出于某种原因,每当我尝试将从 GET 请求获得的 URL 加载到服务器时,它不会加载,但如果我尝试直接加载字符串,它就可以工作。这是我的代码:

new Thread(new Runnable() {
        @Override public void run() {
            try {
                URL obj = new URL(url1 + overallid);
                HttpURLConnection con = (HttpURLConnection) obj.openConnection();

                con.setRequestMethod("GET");

                con.addRequestProperty("User-Agent", "Mozilla/4.76");

                BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
                String inputLine;
                StringBuffer response = new StringBuffer();

                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                in.close();
                System.out.println(response.toString().replaceAll("\s", ""));
                System.out.println("Set pic to: " + pic);
                Picasso.with(LoginActivity.this).load(pic).into(image);
                i++;
                overallid++;
            } catch (Exception ex) {
                System.out.println(ex);
            }
        }
    }).start();

如果我直接制作 pic = a imgur link 它可以工作,但是如果我从 GET 中获取它就不会工作。有什么想法吗?

谢谢, 奎因(融合)

Picasso 应该从主线程调用...试试这个代码:

Handler mainThreadHandler=new Handler();
new Thread(new Runnable() {
    @Override
    public void run() {
        try {
            URL obj = new URL(url1 + overallid);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();

            con.setRequestMethod("GET");

            con.addRequestProperty("User-Agent", "Mozilla/4.76");

            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
            System.out.println(response.toString().replaceAll("\s", ""));
            System.out.println("Set pic to: " + pic);
            mainThreadHandler.post(new Runnable() {
                @Override
                public void run() {
                    Picasso.with(LoginActivity.this).load(pic).into(image);
                }
            });
            i++;
            overallid++;
        } catch (Exception ex) {
            System.out.println(ex);
        }
    }
}).start();

这里是一个处理位图下载的例子。

    BufferedInputStream inputStream = null;
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {
        URL url = new URL("the image url to load");
        HttpURLConnection connection = (HttpURLConnection) url
                .openConnection();

        inputStream = new BufferedInputStream(connection.getInputStream());

        byte[] buffer = new byte[8192];
        int n = -1;
        while ((n = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, n);
        }

        final Bitmap bitmap = BitmapFactory.decodeByteArray(
                outputStream.toByteArray(), 0, outputStream.size());

        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                // TODO handle image here
            }
        });
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }