This is the 17th day of my participation in the August More text Challenge. For details, see:August is more challenging

After upgrading Android 10, I found that the Http request of the project actually failed. After reading the data, I found that in Android 9.0 and above, because of the high security, all applications use HTTPS, OkHttp3 has done the check, so if the plaintext traffic is used, by default, In Android version 9.0 OkHttp3 throws an exception: CLEARTEXT communication to “+ host +” not permitted by network security policy

1. Demote targetSdkVersion to a version below 27, but this will cause your device to no longer run on Android 10.

2, in the manifest. The application of XML file under the label add android: usesCleartextTraffic = “true”, its role is to indicate whether the application intends to use the clear network traffic, such as expressly HTTP, and ignore the Https certificate.

Android: usesCleartextTraffic indicates whether the application intends to use plaintext network traffic, such as plaintext HTTP. The default value is “true” for applications with a target API level of 27 or lower. The default is “False” for applications with API level 28 or higher. When the property is set to “false”, the platform component will reject requests from applications to use plaintext traffic. The main reason to avoid plaintext communication is the lack of confidentiality, authenticity and tamper-proof protection; A network attacker can eavesdrop on the data being transmitted and modify it without detection.

Also need to note is that if there are no android: usesCleartextTraffic = “true”, is not allowed in GooglePlay shelves APK.

3. Create network_security_config.xml in the res/ XML folder

<network-security-config>
    <base-config cleartextTrafficPermitted="true">
        <trust-anchors>
            <certificates src="system" />
        </trust-anchors>
    </base-config>
</network-security-config>

Copy the code

Then the Application tag under new attributes: android: networkSecurityConfig = “@ XML/network_security_config”