Home > Java > javaTutorial > How Can I Allow Both HTTP and HTTPS Connections in My Android 9 Pie App?

How Can I Allow Both HTTP and HTTPS Connections in My Android 9 Pie App?

Patricia Arquette
Release: 2024-12-28 14:24:10
Original
628 people have browsed it

How Can I Allow Both HTTP and HTTPS Connections in My Android 9 Pie App?

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 element in the AndroidManifest.xml file. Setting this attribute to true allows the app to handle all HTTP requests without encryption.

<application android:usesCleartextTraffic="true">
  ...
</application>
Copy after login

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>
Copy after login

Referencing network_security_config.xml in the Manifest

Once you have created the network_security_config.xml file, reference it in the element of the AndroidManifest.xml file using the android:networkSecurityConfig attribute:

<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
  <application android:networkSecurityConfig="@xml/network_security_config">
    ...
  </application>
</manifest>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template