ionic capacitor working in browser and does not work in android

Here is how I resolved when the ionci capacitor is working in the browser and does not work in android.

9 times out of ten this is because you haven’t used https for the API feed. (Yes, I was using http as the API post URL)

http is disabled on android since a while back, unless you set up special security rules.

The rest of the time it’s a CORS issue.

What to do below:

Step 1. Create a file called network_security_config.xml in your project root  inside this path:
app/src/main/res/xml/network_security_config.xml

Then add these lines of code inside in as follow:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>

<base-config cleartextTrafficPermitted=”true”>

<trust-anchors> <certificates src=”system” /> <certificates src=”user” /> </trust-anchors>

</base-config>

<domain-config cleartextTrafficPermitted=”true”>

<domain-config cleartextTrafficPermitted="true">

<domain>YOUR_LOCAL_DOMAIN</domain>

<domain>mydomaoin.com</domain>

</domain-config>

</network-security-config>

 

Step 2.  Goto the app/src/main/AndroidManifest.xml file:

Then add this in the <application> section:

<application

android:networkSecurityConfig=“@xml/network_security_config”

>

 

step 3. (Optional) Also in Your capacitor.config.json file add: set to true

cleartext?: true;

Note:

/** * Allow cleartext traffic in the Web View. * * On Android, all cleartext traffic is disabled by default as of API 28. * * This is intended for use with live-reload servers where unencrypted HTTP * traffic is often used. * * **This is not intended for use in production.** * * @since 1.5.0 * @default false

Then rebuild your apk: