Development with the mind: android network can not link low version can network link, Android P can not be linked after the network reported Cleartext HTTP traffic to xxx not permitted

You may find in the project to maintain their own, in front of models used properly, but to more than Android p models, can not find a network request to the server, and suddenly very awkward.

报错  Cleartext HTTP traffic to xxx not permitted

Of course, as a developer is required to solve such problems.

 

Produce such reasons:

To ensure the security of user data and devices, Google Android system for the next generation (Android P) application, the default will be asked to use an encrypted connection, which means that Android P will prohibit the use of all App unencrypted connections, so running Android P system the Andrews device either receive or send traffic codes can not be transmitted next, the next required (transport layer security) transport layer security protocol.

Use OKHttp request appears

java.net.UnknownServiceException: CLEARTEXT communication ** not permitted by network security policy
on the device Android P system, if the non-encrypted application using the http request plaintext network traffic, the application will not cause the network request, https the is not affected in the same manner, if the application nested webview, webview only use https request.

 

This problem I recommend the solution:

Res folder is created under a xml folder, and then create a network_security_config.xml file, as follows:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true" />
</network-security-config>


2. Subsequently, application tag in the file AndroidManifest.xml add the following properties:

<application
...
 android:networkSecurityConfig="@xml/network_security_config"
...
    />


Completed, this time App can access the network.

If you do not want to be lazy, then modified to do so

(1) APP use https requests

(2)targetSdkVersion 降到27以下

(3)更改网络安全配置

 

注意  build 版本中   compileSdkVersion  需要在  24 以上  

 

想要更多的编程学习资料,请关注微信公众号:IT010101

 

发布了24 篇原创文章 · 获赞 16 · 访问量 3万+

Guess you like

Origin blog.csdn.net/ccc920123/article/details/99723006