当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网络环境下加载图片了!赶紧去试试吧!
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值