当Fresco/Picasso遇到https.(停止更新)

0、停止更新说明:

在新本版的Picasso和Fresco框架都已经支持了对https图片的加载。
导致这篇文章也失去了意义!感谢大家!

当Fresco/Picasso遇到https,原来的图片就加载不出来了。这个应该怎么搞?查了查,进行如下配置:

1、Fresco:

1.1.1如果你使用的是okhttp2:

    在build.gradle中添加依赖:
dependencies {
  // your project's other dependencies
  compile "com.facebook.fresco:imagepipeline-okhttp:0.12.0+"
}

1.1.2如果你使用的是okhttp3:

    在build.gradle中添加依赖:
dependencies {
  // your project's other dependencies
  compile "com.facebook.fresco:imagepipeline-okhttp3:0.12.0+"
}

1.2在application中初始化:

Context context;
OkHttpClient okHttpClient; // build on your own
ImagePipelineConfig config = OkHttpImagePipelineConfigFactory
    .newBuilder(context, okHttpClient)
    .build();
Fresco.initialize(context, config);
导包信息如下:
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.imagepipeline.backends.okhttp3.OkHttpImagePipelineConfigFactory;
import com.facebook.imagepipeline.core.ImagePipelineConfig;

2、Picasso:

2.1这个配置比较简单:在application中初始化,添加如下代码:

        Picasso.setSingletonInstance(new Picasso.Builder(mContext).
                downloader(new OkHttpDownloader(getUnsafeOkHttpClient()))
                .build());

2.2添加如下方法:

  public static OkHttpClient getUnsafeOkHttpClient() {

        try {
            // Create a trust manager that does not validate certificate chains
            final TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
                @Override
                public void checkClientTrusted(
                        java.security.cert.X509Certificate[] chain,
                        String authType) throws CertificateException {
                }

                @Override
                public void checkServerTrusted(
                        java.security.cert.X509Certificate[] chain,
                        String authType) throws CertificateException {
                }

                @Override
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
            }};

            // Install the all-trusting trust manager
            final SSLContext sslContext = SSLContext.getInstance("SSL");
            sslContext.init(null, trustAllCerts,
                    new java.security.SecureRandom());
            // Create an ssl socket factory with our all-trusting manager
            final SSLSocketFactory sslSocketFactory = sslContext
                    .getSocketFactory();

            OkHttpClient okHttpClient = new OkHttpClient();
            okHttpClient.setSslSocketFactory(sslSocketFactory);
            okHttpClient.setHostnameVerifier(new HostnameVerifier() {

                @Override
                public boolean verify(String hostname, SSLSession session) {
                    return true;

                }
            });

            return okHttpClient;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

2.3 但是此处注意:new OkHttpDownloader方法,在导包的时候一定要导入Picasso中的okhttp中的包,不要使用项目中用到的okhttp中的包,具体导入如下:

import com.squareup.picasso.OkHttpDownloader;
import com.squareup.picasso.Picasso;
好了,到这里就能使用Fresco和Picasso在https网络环境下加载图片了!赶紧去试试吧!

你可能感兴趣的:(Android)