【问题标题】:Picasso does not load some URL (included http:// or https://)Picasso 不加载某些 URL(包括 http:// 或 https://)
【发布时间】:2018-04-12 10:17:10
【问题描述】:

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

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

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

为什么我可以用毕加索加载url3? Picasso 不会加载哪些类型的 url?

【问题讨论】:

    标签: android picasso


    【解决方案1】:

    毕加索直接不支持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

    【讨论】:

    • 谢谢,您的解决方案救了我。但是由于我在项目中使用了 Retrofit2,所以我不得不使用 okhttp3 并相应地更新了上述解决方案。但是这种方法在我使用的最新毕加索版本(2.71828)上不起作用,我不得不将毕加索版本降级到2.4作为您的解决方案!我们如何使用 okhttp3 将此解决方案应用于 Picasso 2.71828?
    【解决方案2】:

    您可以使用通用图像加载器来实现这一点。原因是毕加索不支持“https”。 Check out this link for reference.

    【讨论】:

    • OP 要求解决方案,而不是新库推荐并且有解决方案。
    【解决方案3】:

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

    .jpg // working
    .JPG // not working
    

    我想如果你以 .jpg 格式上传相同的图像,它会起作用。

    希望对你有帮助!!

    【讨论】:

    • 这对我的一些图片网址有帮助。
    猜你喜欢
    • 2011-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-05
    • 1970-01-01
    • 2017-12-20
    • 2016-01-19
    • 1970-01-01
    相关资源
    最近更新 更多