Android开发BUG 安卓P(9.0)请求网络适配 not permitted by network security policy

使用retrofit访问http协议的接口时,打印错误信息如下:

CLEARTEXT communication to ***************** not permitted by network security policy

1.http 网络请求的问题:java.net.UnknownServiceException: CLEARTEXT communication ** not permitted by network security policy

解决方法

后端接口改用https协议或添加网络配置文件

.在 res 下新增一个 xml 目录,然后创建一个名为:network_security_config.xml 文件(名字可自定义)
在这里插入图片描述
代码:

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

AndrManiFest.xml中在application下添加:

android:networkSecurityConfig="@xml/network_config"
发布了67 篇原创文章 · 获赞 32 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_42391904/article/details/100906476
今日推荐