3

I have gone through this question link, and I have made subsequent changes but still can't solve the problem. Still giving me the same error. Can someone please help me out. Thanks in advance Here is my piece of code from config.xml

 <widget id="com.farm.fork" version="0.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">


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

network_security_config.xml

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

AndoidManifest.xml

<?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="1" android:versionName="0.0.1" package="com.farm.fork" xmlns:android="http://schemas.android.com/apk/res/android">
    <supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application android:hardwareAccelerated="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:networkSecurityConfig="@xml/network_security_config" android:supportsRtl="true" android:usesCleartextTraffic="true">
        <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|smallestScreenSize|screenLayout|uiMode" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:theme="@android:style/Theme.DeviceDefault.NoActionBar" android:windowSoftInputMode="adjustResize">
            <intent-filter android:label="@string/launcher_name">
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:enabled="true" android:exported="false" android:name="com.google.android.gms.measurement.AppMeasurementService" />
        <service android:name="org.apache.cordova.firebase.FirebasePluginMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
        <receiver android:name="org.apache.cordova.firebase.OnNotificationOpenReceiver" />
        <meta-data android:name="com.google.firebase.messaging.default_notification_color" android:resource="@color/accent" />
        <meta-data android:name="com.google.firebase.messaging.default_notification_channel_id" android:value="@string/default_notification_channel_id" />
        <meta-data android:name="firebase_analytics_collection_enabled" android:value="true" />
        <meta-data android:name="firebase_performance_collection_enabled" android:value="true" />
        <meta-data android:name="firebase_crashlytics_collection_enabled" android:value="true" />
    </application>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-feature android:name="android.hardware.location.gps" />
</manifest>
10
  • Can you post your final Androidmanifest.xml file
    – Swayangjit
    Commented Nov 30, 2019 at 16:27
  • Uploaded androidmanifest.ml file Commented Nov 30, 2019 at 16:40
  • Can you try this <?xml version="1.0" encoding="utf-8"?> <network-security-config> <base-config cleartextTrafficPermitted="true"> <trust-anchors> <certificates src="system" /> </trust-anchors> </base-config> <domain-config cleartextTrafficPermitted="true"> <domain includeSubdomains="true">localhost</domain> </domain-config> </network-security-config> in your network_security_config.xml
    – Swayangjit
    Commented Nov 30, 2019 at 16:59
  • now I am getting this error Failed to load resource: net::ERR_CONNECTION_REFUSED Commented Nov 30, 2019 at 17:22
  • try adding <access origin="" /> <allow-navigation href="" /> in config.xml
    – Swayangjit
    Commented Nov 30, 2019 at 17:26

5 Answers 5

19

After a few days of struggle, this works for me, and I hope this also works for you. add this to your CONFIG.XML, top of your code.

<access origin="*" />
<allow-navigation href="*" />

and this, under the platform 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:usesCleartextTraffic="true" />
     <application android:networkSecurityConfig="@xml/network_security_config" />
 </edit-config>
 <resource-file src="resources/android/xml/network_security_config.xml" 
 target="app/src/main/res/xml/network_security_config.xml" />

add the follow code to this file "resources/android/xml/network_security_config.xml".

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true">
        <trust-anchors>
            <certificates src="system" />
        </trust-anchors>
    </base-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">localhost</domain>
        <domain includeSubdomains="true">YOUR DOMAIN HERE/IP</domain>
    </domain-config>
</network-security-config>

enter image description here

enter image description here

2
  • 3
    Thank you. I was losing my mind on this problem in the last 4 days. Commented Feb 24, 2020 at 10:31
  • 1
    You are the subject.
    – Layer
    Commented Dec 5, 2020 at 4:40
16

Open AndroidManifest.xml and add the following line to <application> tag:

android:usesCleartextTraffic="true"

Refer the image below:

enter image description here

2
  • 1
    thanks. adding android:usesCleartextTraffic="true" t my aplication works fine for me. Commented Sep 1, 2020 at 17:00
  • localhost is not working yet
    – WantToDo
    Commented Jun 6, 2022 at 22:11
1

Try changing your network_security_config.xml

from:

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

to:

<?xml version="1.0" encoding="utf-8"?>    
<network-security-config>
    <base-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">localhost</domain>
    </base-config>
</network-security-config>

notice the change in the tag domain-config to base-config

1
1

As suggested by @Swayangjit I added base config tag in network_security_config.xml.

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

  <base-config cleartextTrafficPermitted="true">
        <trust-anchors>
            <certificates src="system" />
        </trust-anchors>
  </base-config>

    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">192.168.43.147:3000</domain> <!--Ip address-->
        <domain includeSubdomains="true">192.168.43.147:3000/products</domain>
        <domain includeSubdomains="true">10.0.2.2 </domain>

    </domain-config>
</network-security-config>

After that, I was getting connection refused error because I was trying to connect to localhost by mobile which is not possible. Instead, you have to use your IP address. For ex: http://localhost:3000 won't work on the android device instead you have to use the IP address of local network like this

http://IP-addrees:3000. Note: You can find IP address through ipconfig command

1
  • If you are running both server and emulator on your computer, 127.0.0.1 will refer to the emulator itself and not the server. The advantage of using 10.0.2.2 is that you don't care about the real IP so it is easier to share the project while it also works when your machine is not connected to the internet.
    – ofthelit
    Commented Jul 16, 2020 at 19:16
0
This work for me!!

path: ..\resources\android\xml\network_security_config.xml

**<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">localhost</domain>
        <domain includeSubdomains="true">Ip address</domain>
    </domain-config>
</network-security-config>**

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