Glide4.0載入https圖片
阿新 • • 發佈:2018-12-17
1.在Gradle中新增依賴
compile "com.github.bumptech.glide:okhttp3-integration:4.0.0-RC1"
2.新增兩個類
@GlideModule public class OkHttpAppGlideModule extends AppGlideModule { @Override public void registerComponents(Context context, Registry registry) { OkHttpClient client = UnsafeOkHttpClient.getUnsafeOkHttpClient(); registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(client)); } }
public class UnsafeOkHttpClient { public static OkHttpClient getUnsafeOkHttpClient() { 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); } } }
參考連線:
https://juejin.im/entry/5924f28eda2f60005d7725b2
http://blog.csdn.net/alpha58/article/details/61934543