Android P 网络报错 : java.io.IOException: Cleartext HTTP traffic to ***.com not permitted

在Android P 使用HttpUrlConnection进行http请求会出现以下异常

java.io.IOException: Cleartext HTTP traffic to ***.com not permitted
        at com.android.okhttp.HttpHandler$CleartextURLFilter.checkURLPermitted(HttpHandler.java:115)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:458)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:127)

或者 java.net.UnknownServiceException: CLEARTEXT communication ***.com not permitted by network security policy

解决:1. 将Http请求修改为Https请求

2. 新建res/xml/network_security_config.xml


   

在Manifest的application添加android:networkSecurityConfig="@xml/network_security_config"

3.回退targesdk

你可能感兴趣的:(Android P 网络报错 : java.io.IOException: Cleartext HTTP traffic to ***.com not permitted)