HTTP OR HTTPS REQUEST IN ANDROID PIE

Dhruv Singhal
2 min readMar 24, 2022

--

Android Pie (API level 28) came with many new features and NETWORK REQUESTS is one of them. Android PIE uses HTTPS requests by default for every request. So when we develop an android app that uses HTTP network requests the app will work fine in all versions of Android except Android Pie. To run our app in all versions of android we need to fix this problem.

SENDING HTTP REQUEST

The easiest way to solve this problem is to add a few lines of code in the AndroidManifest.XML file.

android:usesCleartextTraffic=”true”

We have to add these lines to the application tag.

<application
android:name=".MainApplication"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:allowBackup="false"
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true"
tools:ignore="GoogleAppIndexingWarning">

But this solution will not work when we have to use HTTP requests for some domains. So we have to do some configurations to make this work.

To make this work in Android PIE we have to define a networkSecurityConfig in the application of our Manifest file tag like this:

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

We have to create a new file inside our XML folder namednetworkSecurityConfig.xml now paste the below code snippet inside the file.

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="false">
<domain includeSubdomains="true">xyz.com</domain>
</domain-config>
</network-security-config>

You can add the domain of your choice in place of xyz.com.

This is it! Now the app will work fine in all Android versions otherwise we would have got no response from the server and users would have uninstalled the app. Besides all this, I just want to say that using an HTTP request is not a good idea as it may lead to some serious security issues.

Thanks for reading! If you enjoyed this story, please click the 👏 button and share it to help others! leave a comment 💬 and valuable suggestions below.

--

--

Dhruv Singhal
Dhruv Singhal

Written by Dhruv Singhal

Data engineer with expertise in PySpark, SQL, Flask. Skilled in Databricks, Snowflake, and Datafactory. Published articles. Passionate about tech and games.

No responses yet