最近在将应用运行在Android 9设备上时,发现http网络请求没有任何的响应。
官方资料在框架安全性变更提及,地址为:
https://developer.android.google.cn/about/versions/pie/android-9.0-changes-28
<https://developer.android.google.cn/about/versions/pie/android-9.0-changes-28>
默认情况下启用网络传输层安全协议 (TLS)
如果您的应用以 Android 9 或更高版本为目标平台,则默认情况下 isCleartextTrafficPermitted()
<https://developer.android.google.cn/reference/android/security/NetworkSecurityPolicy.html#isCleartextTrafficPermitted()>
函数返回 false。 如果您的应用需要为特定域名启用明文,您必须在应用的网络安全性配置
<https://developer.android.google.cn/training/articles/security-config.html>
中针对这些域名将 cleartextTrafficPermitted 显式设置为 true。
具体解决方案共二步,参考资料为:
https://stackoverflow.com/questions/51902629/how-to-allow-all-network-connection-types-http-and-https-in-android-9-pie
<https://stackoverflow.com/questions/51902629/how-to-allow-all-network-connection-types-http-and-https-in-android-9-pie>
第一步:在清单文件AndroidManifest.xml的application标签里面设置networkSecurityConfig属性如下:
<?xml version="1.0" encoding="utf-8"?> <manifest ... > <application
android:networkSecurityConfig="@xml/network_security_config"> <!-- ... -->
</application> </manifest>
第二步:在资源文件夹res/xml下面创建network_security_config.xml如下:
<?xml version="1.0" encoding="utf-8"?> <network-security-config> <base-config
cleartextTrafficPermitted="true"> <trust-anchors> <certificates src="system" />
</trust-anchors> </base-config> </network-security-config>
热门工具 换一换