1、build.gradle添加依赖并执行syncnow
compile "com.github.bumptech.glide:okhttp3-integration:4.0.0-RC1"
2、文件拷贝并删除依赖
1、拷贝依赖库下OkHttpStreamFetcher.java和OkHttpUrlLoader.java俩个文件到项目对应文件夹中
2、删除build.gradle中com.github.bumptech.glide:okhttp3-integration:4.0.0-RC1的依赖
该依赖只为获取上述俩个java类文件
3、定义支持自定义签名证书工具类(可直接拷贝至项目中)
public class HttpsUtil {
public static SSLSocketFactory getSslSocketFactory(){
try{
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new TrustManager[]{new UnSafeTrustManager()}, new SecureRandom());
return sslContext.getSocketFactory();
} catch (NoSuchAlgorithmException e){
throw new AssertionError(e);
} catch (KeyManagementException e){
throw new AssertionError(e);
}
}
public static class UnSafeHostnameVerifier implements HostnameVerifier {
@Override
public boolean verify(String hostname, SSLSession session){
return true;
}
}
private static class UnSafeTrustManager implements X509TrustManager {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType)throws CertificateException {}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType)throws CertificateException {}
@Override
public X509Certificate[] getAcceptedIssuers(){
return new X509Certificate[]{};
}
}
}
4、自定义MyGlideModule实现GlideModule的接口下的registerComponents的方法
@Override
public void registerComponents(@NonNull Context context, @NonNull Glide glide,
@NonNull Registry registry) {
OkHttpClient okhttpClient = new OkHttpClient.Builder()
.retryOnConnectionFailure(true) // 设置出现错误进行重新连接。
.connectTimeout(15, TimeUnit.SECONDS)
.readTimeout(60 * 1000, TimeUnit.MILLISECONDS)
.sslSocketFactory(HttpsUtil.getSslSocketFactory())
.hostnameVerifier(new HttpsUtil.UnSafeHostnameVerifier())
.build();
registry.replace(GlideUrl.class, InputStream.class,
new OkHttpUrlLoader.Factory(okhttpClient));
}
5、mainfest中申明自定义的MyGlideModule
<application>
<!--图片缓存配置-->
<meta-data android:name="packagename.MyGlideModule"
android:value="GlideModule"/>
</application>