1

my ionic 4 HTTP request is not working fine in android 9, but in android < 9 it working perfectly. after debug the app it shows fail status in the network tab. I have added.

<edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application">
            <application android:networkSecurityConfig="@xml/network_security_config" />
            <application android:usesCleartextTraffic="true" />
</edit-config>

and

<widget id="io.ionic.starter" version="1.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:cdv="http://cordova.apache.org/ns/1.0">

in config.xml file and change the android:targetSdkVersion

<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="29" />

in AndroidManifest.xml file. but these are also not useable .please help me out in this.

2 Answers 2

3

I have faced this problem also, but I have a solution that is working in my case. Follow these 4 steps below.

step 1: create a directory named xml in "resources/android/" and create a new file named network_security_config.xml.

step 2: add this following code in this network_security_config file

<?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>

step 3: add this code following code in config.xml file under android platform like below

<platform name="android">

  <edit-config file="app/src/main/AndroidManifest.xml" mode="merge" 
   target="/manifest/application" 
    xmlns:android="http://schemas.android.com/apk/res/android">
        <application 
       android:networkSecurityConfig="@xml/network_security_config" />
        <application android:usesCleartextTraffic="true" />
    </edit-config>
        <resource-file src="resources/android/xml/network_security_config.xml" 
    target="app/src/main/res/xml/network_security_config.xml" />

    <allow-intent href="market:*" />

step 4: don't use targetSdkVersion=29, it will work in 28, use below target version.

 <uses-sdk android:minSdkVersion="19" android:targetSdkVersion="28" />
0
0

--------------- http:// and https:// ---------------

In Android 9, 10, and above versions, they only accept 'HTTPS' requests. If we are debugging our debug build in an Android device(in chrome inspect) having android versions of 5,6,7,8 or lower versions than 5, then we don't feel any issue, and also in lower versions of android accept 'HTTP' requests. This is because the android team had improved its security and not accepting HTTP requests, because as we know that HTTP is a non-secure request.

This was a major problem on which I gets stuck when I was a beginner. And this might be a common mistake (a careless mistake).

For solving this issue then just add an 's' in http:// requests to make it like https:// request

For example -- (if your API link is just like the first one shown below then change your link just like the second one)

1.http://web.global.com/Campus/api/login

2.https://web.global.com/Campus/api/login

I hope this solution will help u in solving the problem and have a great day buddy...

1
  • 2
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Oct 9, 2021 at 8:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.