在Android開發(fā)中,第三方圖片加載庫(kù)使用廣泛,功能強(qiáng)大,其中Glide就是一款非常強(qiáng)大的圖片加載庫(kù),關(guān)于此庫(kù)的一些用法網(wǎng)上比比皆是,這里提一下的是Glide默認(rèn)使用的是HttpURLConnection進(jìn)行網(wǎng)絡(luò)請(qǐng)求,在其Gitgub上的文檔表明,我們可以使用Okhttp、Volley等替換原有的網(wǎng)絡(luò)請(qǐng)求方式。
使用方式:
添加Gradle依賴:
dependencies {
compile 'com.github.bumptech.glide:okhttp3-integration:1.5.0@aar'
//compile 'com.squareup.okhttp3:okhttp:3.2.0'
// but should be compatible with latest as well, if you want to override it
}
在Github上,我們可以看到,在Okhttp3的支持包里,有以下代碼:
@Deprecated
public class OkHttpGlideModule implements GlideModule {
...
@Override
public void registerComponents(Context context, Registry registry) {
//使用定義的OkhttpClient替換原有的請(qǐng)求
registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory());
}
}
接下來我們看OkHttpUrlLoader這個(gè)類里都具體做了什么:
/**
* A simple model loader for fetching media over http/https using OkHttp.
*/
public class OkHttpUrlLoader implements ModelLoader<GlideUrl, InputStream> {
//定義的Client
private final Call.Factory client;
...
@Override
public LoadData<InputStream> buildLoadData(GlideUrl model, int width, int height,
Options options) {
return new LoadData<>(model, new OkHttpStreamFetcher(client, model));
}
/**
* The default factory for {@link OkHttpUrlLoader}s.
*/
public static class Factory implements ModelLoaderFactory<GlideUrl, InputStream> {
private static volatile Call.Factory internalClient;
private Call.Factory client;
private static Call.Factory 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.
*
* @param client this is typically an instance of {@code OkHttpClient}.
*/
public Factory(Call.Factory client) {
this.client = client;
}
...
}
}
這里我們看到,主要提供了兩種方法,來設(shè)置OkhttpClient,而在OkHttpGlideModule中,使用的是默認(rèn)的OkhttpClient,該Client沒有任何自定義的行為,所以我們要實(shí)現(xiàn)添加請(qǐng)求頭的目的,就需要寫一個(gè)子類,繼承OkHttpGlideModule,并重寫registerComponents()方法,設(shè)置我們自定義的OkhttpClient:
public class MyOkHttpGlideModule extends OkHttpGlideModule {
@Override
public void registerComponents(Context context, Glide glide) {
//定制OkHttp
OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
//請(qǐng)求頭設(shè)置
httpClientBuilder.interceptors().add(new HeadInterceptor());
OkHttpClient okHttpClient = httpClientBuilder.build();
glide.register(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(okHttpClient));
}
public static class HeadInterceptor implements Interceptor {
public HeadInterceptor() {
}
@Override
public Response intercept(Chain chain) throws IOException {
Request.Builder request = chain.request().newBuilder();
//這里添加我們需要的請(qǐng)求頭
request.addHeader("Referer", "http://www.baidu.com");
return chain.proceed(request.build());
}
}
}
定義好之后,在Manifest.xml中,添加需要的<meta-data>:
<application>
...
<meta-data
android:name="{packageName}.MyOkHttpGlideModule"
android:value="GlideModule" />
</application>
其他代碼不用動(dòng),接下來,我們使用Glide進(jìn)行網(wǎng)絡(luò)請(qǐng)求時(shí),就會(huì)有請(qǐng)求頭了。