Network request unknown error CLEARTEXT communication to XX not permitted by network security policy Problem solution

Problem:
When making a network request, the log prints

CLEARTEXT communication to XX not permitted by network security policy

Reason:
The Android P system network access security policy has been upgraded, restricting non-encrypted traffic requests.
The Android P system restricts network requests for plaintext traffic. The following versions have no effect, so okhttp3 will throw this exception.

Solution:
Solution 1: Lower the target version, set targetSdkVersion in app/build.gradle to 27 or below.
Solution 2: Change http request to https
Solution 3: Add network security configuration.
1) Create a network_security_config.xml file in res/xml/ of the application, and the file name can be customized.

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

2) Add android:networkSecurityConfig="@xml/network_security_config" to the Application tag in the AndroidManifest.xml file

<application
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:networkSecurityConfig="@xml/network_security_config">
        ...
</application>

Guess you like

Origin blog.csdn.net/yjm_csd/article/details/128319894