Android API 28之后无法HTTP请求,提示Cleartext

问题原因

Android系统出于安全性考虑,在Android P系统(Android API >= 28)的设备上,如果应用使用的是非加密的明文流量的http网络请求,则会导致该应用无法进行网络请求,https则不会受影响;同样的,如果应用嵌套了webview,webview也只能使用https请求。
否则,会报如下的错误:

Cleartext HTTP traffic to xxx not permitted

android工程

在res下新建一个xml目录 创建名为 network_security_config.xml文件 ,该文件内容如下:


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

然后在 AndroidManifest.xml的 application 标签内应用上面的xml配置:

android:networkSecurityConfig="@xml/network_security_config"

Unity工程

如果是Unity,则不需要在res中放一个 network_security_config.xml文件,
直接在 AndroidManifest.xml的 application 标签内配置:

android:usesCleartextTraffic="true"

即可

你可能感兴趣的:(unity3D,android)