Android HTTP request not working in Android Pie
Recently I faced a weird situation where my app was working fine in all Android versions except Android Pie.
After lots of debugging I came to know it was because my server was not secure ie it was using HTTP (not HTTPS). Android P uses HTTPS by default. What this means is that if you are using unencrypted HTTP requests in your app, the app will work fine in all versions of Android except Android P.
Let’s consider two situations where your app won’t work properly in Android P. Firstly, if your server is on HTTP obviously it won’t work in Android P. Another case is when your server is on HTTPS but it is returning something like an image URL which is HTTP, you won’t be able to load the image in Android P.
The good news is… there is a really simple and quick fix for the above problem so let’s get started.
You just need to create a network_security_config file in the xml folder and then include it in the manifest in the following manner.
Steps to find this file in Ionic and Android Project
Go to Platforms -> android -> app -> src ->res -> AndroidManifest.xml
Add this path if not exist android:networkSecurityConfig="@xml/network_security_config"
<?xml version='1.0' encoding='utf-8'?>
<manifest .....>
<application android:networkSecurityConfig="@xml/network_security_config"
........>
</application>
</manifest>
The network_security_config file looks like
To find this file in Ionic Project Go to resources -> android - > xml -> network_security_config
By default your network_security_config file looks like this
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">localhost</domain>
</domain-config>
</network-security-config>
You just do two simple steps
1. Remove
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">localhost</domain>
</domain-config>
2. Replace with this Code
<base-config cleartextTrafficPermitted="true">
<trust-anchors>
<certificates src="system" />
</trust-anchors>
</base-config>
<?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>
comment on my post if you have some problem about content of the post or you face some new issue in Ionic Application or Android Native Application. Fell free to ask question. | |
Comments
Post a Comment