解决Android9.0 http连接无法访问,CLEARTEX
2019-02-04 本文已影响0人
and2long
app
Android P 禁止了http连接?(WTF!数据请求失败,图片也加载不出来)
LogCat错误日志显示:
... CLEARTEXT communication not permitted by network security policy ...
解决方法
最简单的解决方法就是明确指定接受某些主机的明文。
- 添加安全配置文件
<?xml version="1.0" encoding="utf-8"?>
<manifest ... >
<application android:networkSecurityConfig="@xml/network_security_config"
... >
...
</application>
</manifest>
- main -> res -> xml 新建network_security_config.xml
- 允许指定http链接
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config>
<domain includeSubdomains="true">example1.com</domain>
<domain includeSubdomains="true">example2.com</domain>
</domain-config>
</network-security-config>
- 允许所有http链接
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true">
<trust-anchors>
<certificates src="system" />
<certificates src="user" />
</trust-anchors>
</base-config>
</network-security-config>
就这样!
参考文档:
Android P - CLEARTEXT communication not permitted by network security policy
网络安全性配置 | Google Developers