使用SSL离线时使用Picasso加载图像

时间:2017-05-05 11:41:23

标签: android picasso

我实际上在我的项目中使用毕加索。问题是:它似乎没有缓存,因此图像一直在重新加载url(https)。

我想将图像放在磁盘缓存中。 我想要在已经下载图像的情况下缓存图片,而不是取决于WIFI是否关闭。

我创建了一个类( PicassoTrustAll.class )来从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", String.valueOf(exception));
                        }
                    }).build();

        }

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

我这样使用它:

PicassoTrustAll.getInstance(v.getContext())
                .load(image_url)
                .placeholder(R.drawable.progress_animation)
                .into(photo);

1 个答案:

答案 0 :(得分:0)

它正在使用OKHTTP:

Picasso.with(context).load("url")
                        .networkPolicy(NetworkPolicy.OFFLINE) 
                        .into(photo, new Callback() {
                            @Override 
                            public void onSuccess() { 

                            } 

                            @Override 
                            public void onError() {
                                Picasso.with(context) 
                                        .load("url") 
                                .placeholder(R.mipmap.ic_launcher)
                                .error(R.drawable.user_error)
                                        .into(picture); 
                            } 
                        });