Glide加载https图片

        最近公司将项目的图片地址都改为了自签名的https地址,导致之前用Glide加载的图片不能正常显示。于是花了一天时间在网上找相关方法解决该问题。

        首先我们知道Glide支持网络请求库的定制,Glide默认使用HttpConnection来执行网络请求,同时支持OkHttp和Volley的定制。

        Glide网络库的定制非常简单方便,只需要在build.gradle 中添加对应依赖就行,Gradle 会自动合并必要的 GlideModule 到你的Android.Manifest。Glide 会认可在 manifest 中的存在,然后使用对应网库做到所有的网络连接。

OkHttp定制

<span style="font-size:14px;">compile 'com.squareup.okhttp:okhttp:2.7.2'
compile 'com.github.bumptech.glide:glide:3.6.1'
compile 'com.github.bumptech.glide:okhttp-integration:1.4.0@aar'</span>

Volley定制:
<span style="font-size:14px;">compile 'com.github.bumptech.glide:glide:3.6.1'
compile 'com.github.bumptech.glide:volley-integration:1.3.1@aar'
compile 'com.mcxiaoke.volley:library:1.0.8'</span>

下面开始进入主题:Glide定制可加载https的okhttpclient,由于我们项目使用okhttp来完成网络请求,所以这里拿okhttp来介绍流程。

一、定制okhttp网络层

        根据上面介绍,在build.gradle 中添加okhttp对应依赖。

        定制过用okhttp来执行网络后,再用Glide加载https图片,图片依然不能正常显示,将Glide加载的错误日志打印出来(Glide.with(context).load("").listener(lstn)),如下:

<span style="font-size:14px;">javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
                                                             at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:381)
                                                             at com.squareup.okhttp.internal.io.RealConnection.connectTls(RealConnection.java:188)
                                                             at com.squareup.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:145)
                                                             at com.squareup.okhttp.internal.io.RealConnection.connect(RealConnection.java:108)
                                                             at com.squareup.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:184)
                                                             at com.squareup.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:126)
                                                             at com.squareup.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:95)
                                                             at com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:281)
                                                             at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:224)
                                                             at com.squareup.okhttp.Call.getResponse(Call.java:286)
                                                             at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:243)
                                                             at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:205)
                                                             at com.squareup.okhttp.Call.execute(Call.java:80)
                                                             at com.b
  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值