毕加索不加载某些URL(包含http://或https://) [英] Picasso does not load some URL (included http:// or https://)

查看:80
本文介绍了毕加索不加载某些URL(包含http://或https://)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using the Picasso library version 2.71828 to load some image, but it does not work with all URL. Here is my code:

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

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

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

毕加索仅适用于url1url2.即使我可以在浏览器中打开图像,它也不会显示url3图像.

Picasso only works with url1 and url2. It does not display image with url3 even I can open this on browsers.

为什么我要用Picasso加载url3?毕加索无法加载哪些类型的网址?

Why can I load url3 with Picasso? Which types of url that Picasso does not load?

推荐答案

毕加索直接不支持https.所以你必须组合这个包裹.

Picasso directly doesn't support https. so u have to combine this package.

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.

and add picasso custom class to handle 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);

阅读https背后的原因

这篇关于毕加索不加载某些URL(包含http://或https://)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆