Android请求网络报错:not permitted by network security policy

一、错误记录

https的接口请求正常的,
请求http的接口时报错:not permitted by network security policy

二、问题分析

原因:
由于 Android P(版本27以上) 限制了明文流量的网络请求,非加密的流量请求都会被系统禁止掉。如果当前应用的请求是 htttp 请求,而非 https ,这样就会导系统禁止当前应用进行该请求。
也就是Android9.0以上都要https,不能http了,不然拒绝访问。

三、解决方法

1、在res目录下创建一个xml文件夹,在xml文件夹下创建

network_security_config.xml 文件,内容如下:

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

2、在清单文件AndroidManifest.xml中,application下添加

android:networkSecurityConfig="@xml/network_security_config"

重新运行,接口请求就正常了

猜你喜欢

转载自blog.csdn.net/Billy_Zuo/article/details/132673693