Skip to main content

Http network requests are not working in Android Pie

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>


If you like this post. Please share with your friends and help others to solve this problem. You can
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

Popular posts from this blog

How to align Title at center of ActionBar in Android

How to Align Title At Center of Action Bar in Android                                                                                                                                                @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setTitle("DashBoard"); }   Activity  public void setTitle ( String title ){ getSupportActionBar (). setHomeButtonEnabled ( true ); getSupportActionBar (). setDisplayHomeAsUpEnabled ( true ); TextView textView = new TextView ( this ); textView . set...

SQLite Database With Android

Android provides several ways to store user and app data. SQLite is one way of storing user data. SQLite is a very light weight database which comes with Android OS. In this tutorial I’ll be discussing how to write classes to handle all SQLite operations. SQLite is a opensource  SQL database that stores data to a text file on a device. Android comes in with built in SQLite database implementation. SQLite supports all the relation database features. In order to access this database you don't need to establish any kind of connections for it like JDBCODBC etc. In this tutorial I am taking an example of storing user Detail in SQLite database. I am using a table called UserMaster to store user detail. This table contains three columns  id (INT) , userid (VARCHAR) , username (VARCHAR) , password (VARCHAR) , address (VARCHAR) . Database- Package The main package is android.database.sqlite that contains the classes to mange your own database. In this Class we are creat...

Difference Between Pending Intent And Intent in Android

                       Normal Intent       Normal Intent will die as soon as the app being killed.    An Android Intent is an object carrying an intent, i.e a message from one Component to another     Component either inside or outside of the application.Intent can communicate message among     any of the three core Components of an application -- Activities, Services,and BroadcastReceivers.     Two types of Intent in Android   1. Explicit Intent.   2.Implicit Intent  Explicit Intent is an Intent which is used to Call the another component Explicitly in your application  Like :We are calling  Next activity on button click of First activity using Intent Example  // Explicit Intent by specifying its class name Intent i = new Intent ( this , TargetActivity . class ); i . putExtra ( "Key1" , "ABC" ); ...