Question
When using OkHttp to request an http address on Android 9.0 mobile phones, the following error will be reported:
java.net. UnknownServiceException: CLEARTEXT communication to **** not permitted by network security policy
The reason is that after Android 9.0, Google officially explained:
In order to ensure the security of user data and devices, Google will require encrypted connections by default for applications of the next generation Android system (Android P), which means that
Android P will prohibit apps from using all unencrypted connections. Therefore, Android devices running Android P
systems will not be able to send or receive traffic plainly in the future.
Solutions (Three Methods):
- APP change to https request
- argetSdkVersion lowered to below 27 change
- Network security configuration
The Third Method
Create an xml folder under the res folder, then create a network_security_config.xml file in the xml directory with the following contents:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
Add properties to the application tag under the AndroidManifest.xml file
android:networkSecurityConfig="@xml/network_security_config"
After configuration, you can access the http address.
Read More:
- [Solved] java.lang.reflect.InaccessibleObjectException: Unable to make protected java.net.http.HttpRequest()…
- [Solved] AmqpConnectException: java.net.ConnectException: Connection refused: connect
- [Solved] kafka startup Error: java.net.ConnectException: Connection refused
- [Solved] SpringBoot integration with Redis error: java.net.ConnectException: Connection refused: no further information
- Jmeter Error java.net.BindException: Address already in use: connect
- socket java.net.BindException: bind failed: EACCES (Permission denied)
- [Solved] Error running query: MetaException(message:Got exception: java.net.ConnectException Call From XXXX
- [Solved] Android Studio Start Error: Missing essential plugin: org.jetbrains.android Please reinstall Android
- [Solved] net core HTTP Error 500.31 – Failed to load ASP.NET Core runtime HTTP Error 500.30 – ASP.NET Core
- [Solved] Android Studio Compile Error: Could not determine java version from ‘11.0.8‘.
- [Solved] failed to req API:localhost:8848/nacos/v1/ns/instance. code:500 msg: java.net.ConnectException
- [Solved] ViewBinding Error: java.lang.ClassCastException: android.widget.RelativeLayout cannot be
- [Exception] javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path validation failed: java.security.cert.CertPathValidatorException: validity check failed
- [Solved] “error_code“:500,“message“:“IO Error trying to forward REST request: java.net.ConnectException: Connection Refused
- Win assembleDebug Downloading gradle.zip Error: Exception in thread “main” java.lang.RuntimeException: javax.net.ssl.SSLExce
- Caused by: java.net.SocketException: Software caused connection abort: socket write error
- [Solved] Android Error: java.lang.IllegalStateException: Not allowed to start service Intent
- [Solved] Android DataBinding Error: java.lang.ArrayIndexOutOfBoundsException: length=2; index=2 fragment Changes to Fragm
- [Solved] Android mediaplayer.prepare() Error: Caused by: java.lang.IllegalStateException
- [Solved] Compile the program error: undefined reference to `cv::dnn::dnn4_v20210301::Net::~Net()‘