Glide 加载https 图片

作者: sirai | 来源:发表于2018-06-05 19:23 被阅读1406次

    1.重写需要的类:

    public class OkHttpGlideModule implements GlideModule {
    @Override
    public void applyOptions(Context context, GlideBuilder builder) {
        // Do nothing.
    }
    
     @Override
        public void registerComponents(Context context, Glide glide, Registry registry) {
            OkHttpClient client = UnsafeOkHttpClient.getUnsafeOkHttpClient();
            registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(client));
        }
    
    
    static 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);
                }
            }
        }
    

    相关文章

      网友评论

        本文标题:Glide 加载https 图片

        本文链接:https://www.haomeiwen.com/subject/bmpmsftx.html