1

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?

Qk Lahpita
  • 427
  • 2
  • 10
  • 18

4 Answers4

9

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

Ramchandra Singh
  • 530
  • 4
  • 15
  • 1
    Thanks, your solution saved me. But Since I had used Retrofit2 in my project, I had to use okhttp3 and updated the above solution accordingly. But this method didn't work on the latest Picasso version (2.71828) that I use, and I had to downgrade Picasso version to 2.4 as your solution! How we can apply this solution for Picasso 2.71828 with okhttp3? – Reyhane Farshbaf Nov 08 '19 at 15:16
2

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

Amit Jangid
  • 2,741
  • 1
  • 22
  • 28
1

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 !!

Anonymous
  • 2,184
  • 15
  • 23
1

Update on Accepted Answer:

Because of my different version of okhttp, I changed some parts of accepted answer like using 'setHostnameVerifier' and 'setSslSocketFactory' functions.

So if have some problems with accepted answer, use this update:

public class PicassoTrustAll {

    private static Picasso mInstance = null;

    private PicassoTrustAll(Context context) {
        OkHttpClient client = null;

        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 = new OkHttpClient.Builder() 
                    .hostnameVerifier(new HostnameVerifier() {
                        @Override
                        public boolean verify(String hostname, SSLSession session) {
                            return true;
                        }
                    }).sslSocketFactory(sc.getSocketFactory(), (X509TrustManager) trustAllCerts[0])
                    .build();

        } catch (Exception e) {
            e.printStackTrace();
        }

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

    }

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