Android version of the networked high failure error: Cleartext HTTP traffic to xxx not permitted Solution

Reprinted: https://blog.csdn.net/gengkui9897/article/details/82863966

Introduction: In order 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 Andrews device system either receive or send traffic codes can not be transmitted next, the next required (transport layer security) protocol transport layer security, while Android Nougat and Oreo not affected.

Therefore, the use of HttpUrlConnection be http request in Android P following exception

 W/System.err: java.io.IOException: Cleartext HTTP traffic to **** not permitted

Use OKHttp request appears

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

In the device Android P system, if the network requests http unencrypted plaintext traffic application used, the application will not cause the network request, https are not affected in the same manner, if the application nested webview, webview You can only use https request.

To address this problem, the following three solutions:

(1) APP use https requests

(2) targetSdkVersion down to 27 or less

(3) change the network security configuration

The first two methods readily understood and implemented, some comments third method, the network security configuration change.

1. Create a folder under the res 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.

------ Update ------

Method four: the <application> tag directly into AndroidManifest.xml profile (thanks junbs share)

    Android: usesCleartextTraffic = "to
true" ----------------
Disclaimer: This article is CSDN blogger "grapefruit king." original article, follow the CC 4.0 BY-SA copyright agreement, reprint Please include links to the original source and this statement.
Original link: https: //blog.csdn.net/gengkui9897/article/details/82863966

Released three original articles · won praise 0 · Views 1563

Guess you like

Origin blog.csdn.net/qizhenyibao/article/details/104299717