Android glide加载https图片失败,添加证书信任(不适用okhttp信任所有证书)

在项目当中出现加载https图片失败,但是将图片url直接复制到浏览器是可以正常访问的,如果你也是出现类似问题可以参考如下解决方案。

首先出现这个问题的原因是因为https协议必须要有CA证书才可以访问,所以问题的本质是我们在通过https请求进行网络访问过程中没有CA证书导致的,所以首先我们要做的是需要把图片https所对应的CA证书下载下来

我在项目当中使用Glide的版本是

implementation 'com.github.bumptech.glide:glide:4.11.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'

尝试过
解决方案 一:
在chrome打开你图片的https的网站, 点击前面那个小锁,然后点击证书
方案二 :
重写 AppGlideModule
清单中配置
<meta-data
android:name="com.test.android.loader.MyGlideModule"
android:value="GlideModule" />
没有解决问题 ,而且处理起来很复杂

最终解决方案
在manifest 中 application位置添加
android:networkSecurityConfig="@xml/network_security_config"
android:usesCleartextTraffic="true"

image.png

放在res目录下 的新建xml文件夹 新增network_security_config 文件

<network-security-config xmlns:tools="http://schemas.android.com/tools"
    tools:ignore="MissingDefaultResource">
    <base-config cleartextTrafficPermitted="true">
        <trust-anchors>
            <certificates src="system" overridePins="true" />
            <certificates src="user" overridePins="true" />
        </trust-anchors>
    </base-config>
</network-security-config>```
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容