Allowing All Network Connections in Android 9 Pie: HTTP and HTTPS
In Android 9 Pie, encrypted connections using TLS are enforced by default, making unencrypted requests unsuccessful. To accommodate apps that need to handle requests over different connection types, Android provides several options for enabling both HTTP and HTTPS connections.
Using AndroidManifest.xml
The simplest method is to add the android:usesCleartextTraffic attribute to the
<application android:usesCleartextTraffic="true"> ... </application>
Using network_security_config.xml
For more fine-tuned control, Android 9 Pie introduced the networkSecurityConfig resource. This file allows you to specify custom network security configurations for your app. To enable cleartext traffic for all requests, create a file named network_security_config.xml in the res/xml directory with the following contents:
<?xml version="1.0" encoding="utf-8"?> <network-security-config> <base-config cleartextTrafficPermitted="true"> <trust-anchors> <certificates src="system" /> </trust-anchors> </base-config> </network-security-config>
Referencing network_security_config.xml in the Manifest
Once you have created the network_security_config.xml file, reference it in the
<?xml version="1.0" encoding="utf-8"?> <manifest ...> <application android:networkSecurityConfig="@xml/network_security_config"> ... </application> </manifest>
By implementing these changes, your app will be able to make requests over both HTTP and HTTPS connections in Android 9 Pie, ensuring compatibility with both types of network interactions.
The above is the detailed content of How Can I Allow Both HTTP and HTTPS Connections in My Android 9 Pie App?. For more information, please follow other related articles on the PHP Chinese website!