Picasso does not load some URL (included http:// or https://)

做~自己de王妃 提交于 2020-01-25 03:57:26

问题


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: http://images.foody.vn/res/g14/138986/prof/s576x330/foody-mobile-a2-jpg-261-635682356468932282.jpg

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

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

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


回答1:


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'

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;
    }
}

Finally use class like That.

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

Please check This reference

Read Reason behind https




回答2:


You can use Universal Image Loader to achieve this. The reason is Picasso doesn't support "https". Check out this link for reference.




回答3:


The only difference I could see is the format.
For clarification

.jpg // working
.JPG // not working

I guess if you upload the same image in .jpg it would work.

Hope it helps !!



来源:https://stackoverflow.com/questions/49793979/picasso-does-not-load-some-url-included-http-or-https

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!