I have already enabled cors in my rest API. I can succesfully make a request in my browser, but when i run my application in android studio using this command "ionic cap run android --livereload --external", i cannot get any response from my api. when checking the console i got below error "net: ERR_CONNECTION_REFUSED"
-
Does this answer your question? [Ionic 5/Capacitor ERR\_CLEARTEXT\_NOT\_PERMITTED in Android](https://stackoverflow.com/questions/60906953/ionic-5-capacitor-err-cleartext-not-permitted-in-android) – Najam Us Saqib Dec 05 '20 at 15:07
-
i have already done this, still cannot make a requeset – luna moonfang Dec 05 '20 at 16:15
-
What Errors are coming? – Najam Us Saqib Dec 05 '20 at 16:26
-
@NajamUsSaqib bro this is also my problem i cannot log any error in android studio, so im blindly solving this issue. i have tried using remote debugging in chrome, but i cannot see my device – luna moonfang Dec 05 '20 at 16:55
-
Have you enabled Developer option in your mobile device? – Najam Us Saqib Dec 05 '20 at 16:58
-
yes i have, also i choose allow file transfer – luna moonfang Dec 05 '20 at 17:00
-
do you have ADB installed? try command `adb devices` to see connected devices on on terminal or CMD. – Najam Us Saqib Dec 05 '20 at 17:02
-
i have installed ADB and was able to see the error now upon checking the dev tools, i see this error: "net: ERR_CONNECTION_REFUSED", also i have check in my api and there is no requests that went through, do you have any idea on what wizardy is going on around here!!!! – luna moonfang Dec 05 '20 at 17:56
2 Answers
If you want to test your app on Android Studio and your API is running on localhost, you need to send requests to 10.0.2.2 instead of localhost. In your case, instead of using the base API URL http://localhost:3000 in your Ionic app, use http://10.0.2.2:3000.
Next, add android:networkSecurityConfig="@xml/network_security_config"
to the application element in the AndroidManifest.xml file:
<application
android:networkSecurityConfig="@xml/network_security_config"
You will need your IPv4 Address. Open Command Prompt, enter ipconfig
and take note of the IPv4 Address.
Create a file named network_security_config.xml in the path android\app\src\main\res\xml and replace ipv4address with your IPv4 Address:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">localhost</domain>
<domain includeSubdomains="true">10.0.2.2</domain>
<domain includeSubdomains="true">ipv4address</domain>
</domain-config>
</network-security-config>
Finally, run your app with the following command:
ionic cap run android -l --host=0.0.0.0
Source: https://developer.android.com/studio/run/emulator-networking.html

- 31
- 4
You have to set android:usecleartexttraffic
attributes in the config.xml.
<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>
...
</platform>
Also, you have to add subDomains of RestAPI in network_security_config.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">localhost</domain>
<domain includeSubdomains="true">127.0.0.1</domain>
</domain-config>
</network-security-config>

- 16
- 3
-
Greetings i have added above configuration still got the "net: ERR_CONNECTION_REFUSED", – luna moonfang Dec 06 '20 at 05:47
-
May I see your added xml file? Have you tried to run it via Android SDK? – Muramoto Hideyosi Dec 08 '20 at 00:20