Glide加载Https图片出错,解决:Glide+okhttp

直接使用Glide不能加载https图片,需要加入okhttp依赖,并重写AppGlideModule

  • Android studio版本:3.5;Glide版本4.9
  • 过程简单,只需重写AppGlideModule(记得注解)
  • 引入glide和okhttp依赖
  • 引入"com.github.bumptech.glide:okhttp3-integration:4.9.0"依赖是为了使用OkHttpUrlLoader 、OkHttpStreamFetcher这两个类
  • 引入“ndroidx.annotation:annotation:1.0.0”依赖是为了解决glide找不到库的报错
一,添加依赖
        //解决Glide找不到Android声明库问题
    annotationProcessor'androidx.annotation:annotation:1.0.0'
        //glide加载http https
    implementation"com.github.bumptech.glide:okhttp3-integration:4.9.0"
    implementation 'com.github.bumptech.glide:glide:4.9.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
二,新建BaseGlide,重写AppGlideModule
//GlideModule注解,实现glideModule的自动实例化和自动注册
@GlideModule
public class BaseGlide extends AppGlideModule {
    @Override
    public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull 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了

你可能感兴趣的:(Android,bug)