毕加索不加载一些URL(包括http://或https://)

时间:2018-04-12 10:17:10

标签: android picasso

我正在使用Picasso库版本2.71828来加载一些图像,但它不适用于所有URL。这是我的代码:

Picasso.get().load(url).into(imageView);

url1https://res.cloudinary.com/lastminute/image/upload/c_scale,w_630/v1431701424/52347407_Casino_Tower_2100x1400_pyzvxz.jpg

url2http://images.foody.vn/res/g14/138986/prof/s576x330/foody-mobile-a2-jpg-261-635682356468932282.jpg

url3https://static3.mytour.vn/resources/pictures/hotels/19/large_vlj1419841660_khach-san-gia-han.JPG

Picasso仅适用于url1url2。即使我可以在浏览器上打开它,它也不会显示url3的图像。

为什么我可以用毕加索加载url3?毕加索没有加载哪种类型的网址?

3 个答案:

答案 0 :(得分:4)

Picasso直接不支持https。所以你必须结合这个包。

compile 'com.squareup.okhttp:okhttp:2.2.0'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.2.0'
compile 'com.squareup.picasso:picasso:2.4.0'

并添加picasso自定义类来处理https。

public class PicassoTrustAll {

    private static Picasso mInstance = null;

    private PicassoTrustAll(Context context) {
        OkHttpClient client = new OkHttpClient();
        client.setHostnameVerifier(new HostnameVerifier() {
            @Override
            public boolean verify(String s, SSLSession sslSession) {
                return true;
            }
        });
        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
            @Override
            public void checkClientTrusted(
                    java.security.cert.X509Certificate[] x509Certificates,
                    String s) throws java.security.cert.CertificateException {
            }

            @Override
            public void checkServerTrusted(
                    java.security.cert.X509Certificate[] x509Certificates,
                    String s) throws java.security.cert.CertificateException {
            }

            @Override
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return new java.security.cert.X509Certificate[] {};
            }
        } };
        try {
            SSLContext sc = SSLContext.getInstance("TLS");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            client.setSslSocketFactory(sc.getSocketFactory());
        } catch (Exception e) {
            e.printStackTrace();
        }

        mInstance = new Picasso.Builder(context)
                .downloader(new OkHttpDownloader(client))
                .listener(new Picasso.Listener() {
                    @Override
                    public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) {
                        Log.e("PICASSO", exception);
                    }
                }).build();

    }

    public static Picasso getInstance(Context context) {
        if (mInstance == null) {
             new PicassoTrustAll(context);
        }
        return mInstance;
    }
}

最后使用类似那样的课程。

PicassoTrustAll.getInstance(context)
                .load(url)
                .into(imageView);

Please check This reference

Read Reason behind https

答案 1 :(得分:1)

您可以使用Universal Image Loader来实现此目的。原因是毕加索不支持“https”。 Check out this link for reference.

答案 2 :(得分:0)

我能看到的唯一区别是格式 澄清

.jpg // working
.JPG // not working

我猜如果你用.jpg上传相同的图像就行了。

希望它有所帮助!!