Android 9 使用okhttp3报错 CLEARTEXT communication to XXXXXXXX not permitted by network security policy

Android 9 使用okhttp3报错 CLEARTEXT communication to XXXXXXXX not permitted by network security policy

问题原因:

应用官方的说明:在 Android 6.0 中,我们取消了对 Apache HTTP 客户端的支持。 从 Android 9 开始,默认情况下该内容库已从 bootclasspath 中移除且不可用于应用。且Android 9 限制了明文流量的网络请求,非加密的流量请求都会被系统禁止掉。

解决方案1:

将http转换成https

解决方案2:

在res目录下新建xml文件夹,文件夹中新建文件network_security_config.xml,文件内容如下

<network-security-config>
    <base-config cleartextTrafficPermitted="true" />
</network-security-config>

在AndroidManifest.xml文件中,Application标签下添加如下属性:

android:networkSecurityConfig="@xml/network_security_config"


版权声明:本文为LoveStarbucks原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。