前言
Glide默認(rèn)加載http或者通過CA認(rèn)證了的https圖片都是沒問題的,但是當(dāng)加載自簽名的https圖片的時候就會報如下錯誤(證書路徑驗證異常)拧烦。
一忘闻、原理
對于加載自簽名的https圖片,我們需要通過GlideModule來進(jìn)行網(wǎng)絡(luò)請求庫的定制恋博。如果你使用的是OkHttp網(wǎng)絡(luò)請求庫齐佳,則需要在app的build.gradle 中添加如下依賴:
compile 'com.github.bumptech.glide:okhttp-integration:1.4.0@aar'
使用的是Volley網(wǎng)絡(luò)請求庫,則添加依賴為:
compile 'com.github.bumptech.glide:volley-integration:1.3.1@aar'
這里主要講使用OkHttp網(wǎng)絡(luò)請求庫的時候如何通過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實現(xiàn)GlideModule,實現(xiàn)該類主要有2個方法:
applyOptions方法主要是處理圖片質(zhì)量炼吴,緩存等。
registerComponents方法主要是改變Glide的網(wǎng)絡(luò)棧疫衩,讓它能從自簽名HTTPS服務(wù)器接收連接和圖片缺厉。
我們看到在registerComponents方法中new OkHttpUrlLoader.Factory()方法創(chuàng)建了一個okhttpclient對象,點進(jìn)去看見還有一個相同的帶參數(shù)的方法
public Factory(OkHttpClient client) {
this.client = client;
}
這里可以傳入一個我們自己的okhttpclient對象隧土,所以如果將我們已經(jīng)通過認(rèn)證的okhttpclient傳進(jìn)來替換這個對象就可以解決提针。
二、解決步驟
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方法需要傳入已經(jīng)通過認(rèn)證的okhttpclient進(jìn)來替換。如何配置通過認(rèn)證的okhttpclient看我的另一篇文章:Android使用OkHttp請求自簽名的https網(wǎng)站
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網(wǎng)站找圖片)
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()為已經(jīng)通過認(rèn)證的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