美文网首页
CLEARTEXT communication to * not

CLEARTEXT communication to * not

作者: Everglow_111 | 来源:发表于2019-07-24 15:39 被阅读0次
image.png

原因是Android P系统限制了明文流量的网络请求,之下的版本没有影响,所以okhttp3会抛出该异常。

解决方法(亲测有效):
在res目录下新建xml文件夹,文件夹里面创建network_security_config.xml 文件;
文件内容

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true" />
</network-security-config>

然后在 AndroidManifest.xml 的application 标签加上

 <application
        android:networkSecurityConfig="@xml/network_security_config"
        android:name=".DmgApplication"
        android:allowBackup="true"
        android:icon="@mipmap/icon"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/icon"
        android:supportsRtl="true"
        android:theme="@style/AppTheme.NoActionBar">

或者application 加这个属性 android:usesCleartextTraffic="true"

 <application
        android:usesCleartextTraffic="true"
        android:name=".DmgApplication"
        android:allowBackup="true"
        android:icon="@mipmap/icon"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/icon"
        android:supportsRtl="true"
        android:theme="@style/AppTheme.NoActionBar">

相关文章

网友评论

      本文标题:CLEARTEXT communication to * not

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