Android Studio上报错java.net.UnknownServiceException: CLEARTEXT communication ** not permitted by
因为安全原因,Google针对Android 9.0 的应用程序,将要求默认使用加密连接。
因此在Android 9.0 使用HttpUrlConnection进行http请求会出现以下异常
W/System.err: java.io.IOException: Cleartext HTTP traffic to **** not permitted
使用OKHttp请求则出现
java.net.UnknownServiceException: CLEARTEXT communication ** not permitted by network security policy
针对这个问题,有以下三种解决方法:
(1)APP改用https请求
(2)targetSdkVersion 降到27以下
将红框内原本的28全部改成26即可
(3)更改网络安全配置
a.在res文件夹下创建一个xml文件夹,然后创建一个network_security_config.xml文件,文件内容如下:
<?xml version="1.0" encoding="utf-8"?> <network-security-config> <base-config cleartextTrafficPermitted="true" /> </network-security-config>
b.接着,在AndroidManifest.xml文件下的application标签增加以下属性:
https://blog.****.net/SmileToLin/article/details/83651680