Android开发:Glide基本使用以及加载https图片(配合OkHttp实现)

方法一、

参考:Glide基本使用以及加载https图片(配合OkHttp实现)-CSDN博客

方法二、

如果不想使用@GlideModule,使用下面的代码也可以正常使用【需要依赖方法一中的四个仓库】

Glide.get(Utils.getApp()).registry.replace<GlideUrl, InputStream>(

GlideUrl::class.java,

InputStream::class.java,

OkHttpUrlLoader.Factory(UnsafeOkHttpClient.getUnsafeOkHttpClient())

)

复制代码
package com.telecomyt.contact.utils;

import java.util.concurrent.TimeUnit;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import okhttp3.OkHttpClient;


/**
 * Created by zhuguohui on 2018/7/11.
 */

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) {
                        }

                        @Override
                        public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) {
                        }

                        @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);
        }
    }
}
相关推荐
susu10830189112 小时前
Android Studio打包APK
android·ide·android studio
2401_897907863 小时前
Android 存储进化:分区存储
android
Dwyane0310 小时前
Android实战经验篇-AndroidScrcpyClient投屏一
android
FlyingWDX10 小时前
Android 拖转改变视图高度
android
_可乐无糖10 小时前
Appium 检查安装的驱动
android·ui·ios·appium·自动化
一名技术极客12 小时前
Python 进阶 - Excel 基本操作
android·python·excel
我是大佬的大佬13 小时前
在Android Studio中如何实现综合实验MP3播放器(保姆级教程)
android·ide·android studio
lichong95113 小时前
【Flutter&Dart】MVVM(Model-View-ViewModel)架构模式例子-http版本(30 /100)
android·flutter·http·架构·postman·win·smartapi
刘争Stanley13 小时前
Android系统开发(六):从Linux到Android:模块化开发,GKI内核的硬核科普
android·linux·运维·内核·镜像·gki·kmi
五味香13 小时前
Java学习,List截取
android·java·开发语言·python·学习·golang·kotlin