前言
Glide默认加载http或者通过CA认证了的https图片都是没问题的,但是当加载自签名的https图片的时候就会报如下错误(证书路径验证异常)。
一、原理
对于加载自签名的https图片,我们需要通过GlideModule来进行网络请求库的定制。如果你使用的是OkHttp网络请求库,则需要在app的build.gradle 中添加如下依赖:
compile 'com.github.bumptech.glide:okhttp-integration:1.4.0@aar'
使用的是Volley网络请求库,则添加依赖为:
compile 'com.github.bumptech.glide:volley-integration:1.3.1@aar'
这里主要讲使用OkHttp网络请求库的时候如何通过GlideModule来定制我们的Glide。
通过添加的依赖源码其中一个类OkHttpGlideModule 可以看出,
public class OkHttpGlideModule implements GlideModule {
@Override
public void applyOptions(Context context, GlideBuilder builder) {
// Do nothing.
}
@Override
public void registerComponents(Context context, Glide glide) {
glide.register(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory());
}
}
源码是通过自定义一个类OkHttpGlideModule实现GlideModule,实现该类主要有2个方法:
applyOptions方法主要是处理图片质量,缓存等。
registerComponents方法主要是改变Glide的网络栈,让它能从自签名HTTPS服务器接收连接和图片。
我们看到在registerComponents方法中new OkHttpUrlLoader.Factory()方法创建了一个okhttpclient对象,点进去看见还有一个相同的带参数的方法
public Factory(OkHttpClient client) {
this.client = client;
}
这里可以传入一个我们自己的okhttpclient对象,所以如果将我们已经通过认证的okhttpclient传进来替换这个对象就可以解决。
二、解决步骤
1、在build.gradle中添加依赖:
compile 'com.squareup.okhttp3:okhttp:3.6.0'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.github.bumptech.glide:okhttp-integration:1.4.0@aar'
2、拷贝'com.github.bumptech.glide:okhttp-integration:1.4.0@aar'里面3个类OkHttpGlideModule ,OkHttpUrlLoader,OkHttpStreamFetcher到项目中(三个类源码,文章末尾贴出)
注意:OkHttpGlideModule 类的registerComponents方法需要传入已经通过认证的okhttpclient进来替换。如何配置通过认证的okhttpclient看我的另一篇文章:Android使用OkHttp请求自签名的https网站
3、删除在build.gradle中的依赖:
compile 'com.github.bumptech.glide:okhttp-integration:1.4.0@aar'
4、在AndroidManifest中配置刚刚拷贝过来的OkHttpGlideModule。
<!--配置glide加载https所需要的GlideModule-->
<meta-data
android:name="com.alpha58.okhttps.https.OkHttpGlideModule"
android:value="GlideModule"/>
好了,这个时候我们用Glide就可以将自签名的https图片加载出来啦!
public void getHttpsImg(View view) {
//自签名https图片链接 (如果链接失效,自行到12306网站找图片)
String url = "https://travel.12306.cn/imgs/resources/uploadfiles/images/a9b9c76d-36ba-4e4a-8e02-9e6a1a991da0_news_W540_H300.jpg";
Glide.with(this)
.load(url)
.asBitmap()
.into(mIv_img);
}
贴上三个类源码,分别如下:
OkHttpGlideModule类:
public class OkHttpGlideModule implements GlideModule {
@Override
public void applyOptions(Context context, GlideBuilder builder) {
// Do nothing.
}
@Override
public void registerComponents(Context context, Glide glide) {
//注意:new HTTPSUtils(context).getInstance()为已经通过认证的okhttpclient
glide.register(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(new HTTPSUtils(context).getInstance()));
}
}
OkHttpUrlLoader类:
public class OkHttpUrlLoader implements ModelLoader<GlideUrl, InputStream> {
/**
* The default factory for {@link OkHttpUrlLoader}s.
*/
public static class Factory implements ModelLoaderFactory<GlideUrl, InputStream> {
private static volatile OkHttpClient internalClient;
private OkHttpClient client;
private static OkHttpClient getInternalClient() {
if (internalClient == null) {
synchronized (Factory.class) {
if (internalClient == null) {
internalClient = new OkHttpClient();
}
}
}
return internalClient;
}
/**
* Constructor for a new Factory that runs requests using a static singleton client.
*/
public Factory() {
this(getInternalClient());
}
/**
* Constructor for a new Factory that runs requests using given client.
*/
public Factory(OkHttpClient client) {
this.client = client;
}
@Override
public ModelLoader<GlideUrl, InputStream> build(Context context, GenericLoaderFactory factories) {
return new OkHttpUrlLoader(client);
}
@Override
public void teardown() {
// Do nothing, this instance doesn't own the client.
}
}
private final OkHttpClient client;
public OkHttpUrlLoader(OkHttpClient client) {
this.client = client;
}
@Override
public DataFetcher<InputStream> getResourceFetcher(GlideUrl model, int width, int height) {
return new OkHttpStreamFetcher(client, model);
}
}
OkHttpStreamFetcher类:
public class OkHttpStreamFetcher implements DataFetcher<InputStream> {
private final OkHttpClient client;
private final GlideUrl url;
private InputStream stream;
private ResponseBody responseBody;
public OkHttpStreamFetcher(OkHttpClient client, GlideUrl url) {
this.client = client;
this.url = url;
}
@Override
public InputStream loadData(Priority priority) throws Exception {
Request.Builder requestBuilder = new Request.Builder()
.url(url.toStringUrl());
for (Map.Entry<String, String> headerEntry : url.getHeaders().entrySet()) {
String key = headerEntry.getKey();
requestBuilder.addHeader(key, headerEntry.getValue());
}
Request request = requestBuilder.build();
Response response = client.newCall(request).execute();
responseBody = response.body();
if (!response.isSuccessful()) {
throw new IOException("Request failed with code: " + response.code());
}
long contentLength = responseBody.contentLength();
stream = ContentLengthInputStream.obtain(responseBody.byteStream(), contentLength);
return stream;
}
@Override
public void cleanup() {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
// Ignored
}
}
if (responseBody != null) {
responseBody.close();
}
}
@Override
public String getId() {
return url.getCacheKey();
}
@Override
public void cancel() {
// TODO: call cancel on the client when this method is called on a background thread. See #257
}
}
Demo地址:https://github.com/wildma/okhttps
参考文章:Glide — Customize Glide with Modules
网友评论