Glide|Glide 加载https的图片
1、首先在moudle 的build.gradle中添加依赖
implementation'com.squareup.okhttp3:okhttp:3.6.0'
【Glide|Glide 加载https的图片】implementation'com.github.bumptech.glide:glide:4.4.0'
annotationProcessor'com.github.bumptech.glide:compiler:4.4.0'
implementation 'com.github.bumptech.glide:okhttp3-integration:4.0.0-RC1'
2、新建一个类
/**
* author:lyb
* time:2018/1/11 001116:43
* desc :
*/
@GlideModule
public class MyGlideMoudleextends AppGlideModule {
@Override
public boolean isManifestParsingEnabled() {
return false;
}
@Override
public void registerComponents(Context context, Glide glide, Registry registry) {
super.registerComponents(context, glide, registry);
OkHttpClient client = UnsafeOkHttpClient.getUnsafeOkHttpClient();
registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(client));
}
public static class UnsafeOkHttpClient {
public static OkHttpClientgetUnsafeOkHttpClient() {
try {
// Create a trust manager that does not validate certificate chains
final TrustManager[] trustAllCerts =new TrustManager[]{
new X509TrustManager() {
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType)throws CertificateException {
}
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType)throws CertificateException {
}
@Override
public java.security.cert.X509Certificate[]getAcceptedIssuers() {
return new java.security.cert.X509Certificate[]{};
}
}
};
// Install the all-trusting trust manager
final SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
// Create an ssl socket factory with our all-trusting manager
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
OkHttpClient.Builder builder =new OkHttpClient.Builder();
builder.sslSocketFactory(sslSocketFactory, (X509TrustManager) trustAllCerts[0]);
builder.hostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
builder.connectTimeout(20, TimeUnit.SECONDS);
builder.readTimeout(20,TimeUnit.SECONDS);
OkHttpClient okHttpClient = builder.build();
return okHttpClient;
}catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}
3、这样就完成了Glide对证书的支持:GlideApp.with(this).load("https://").into(img);
推荐阅读
- 画解算法(1.|画解算法:1. 两数之和)
- 使用composer自动加载类文件
- ts泛型使用举例
- https请求被提早撤回
- 拒绝可以很艺术,或者很行为艺术。
- www和https://又被Chrome地址栏隐藏了
- AnyProxy抓取http/https请求
- 众泰T500智能互联双加载,让汽车生活更有趣
- ImageLoaders 加载图像
- 计算机网络基础TCP\HTTP\HTTPS