背景: 还是在调用远程接口,进行网络通信的时候,报了如下错误:
W/System.err: java.net.UnknownServiceException: CLEARTEXT
communication to xxx.xxx.xxx not permitted by network security policy
原因:
为保证用户数据和设备的安全,Google针对下一代 Android 系统(Android P) 的应用程序,将要求默认使用加密连接,这意味着 Android P 将禁止 App 使用所有未加密的连接,因此运行 Android P 系统的安卓设备无论是接收或者发送流量,未来都不能明码传输,需要使用下一代(Transport Layer Security)传输层安全协议,而 Android Nougat 和 Oreo 则不受影响。
在Android P系统的设备上,如果应用使用的是非加密的明文流量的http网络请求,则会导致该应用无法进行网络请求,https则不会受影响,同样地,如果应用嵌套了webview,webview也只能使用https请求。
解决办法:
(1)APP改用https请求
(2)targetSdkVersion 降到27以下
(3)在 application 元素中添加:
android:usesCleartextTraffic=”true”
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher"
android:supportsRtl="true"
android:name=".MyApplication"
android:requestLegacyExternalStorage="true"
android:usesCleartextTraffic="true"
android:theme="@style/AppTheme">
————————————————
版权声明:本文为CSDN博主「孤矗」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/zhang33565417/article/details/102806236