最近公司将项目的图片地址都改为了自签名的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>
<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