Android Emulation App Cannot See Network

Hi, perhaps someone can save me from bashing my head against this problem for another 10 hours. I have made an ionic app that communicates with a local server on my pc. This works when I ng serve or ionic serve it. I have since run it in the android emulator and the the network is entirely bugged or not working for the ionic app when running inside the android emulator.

Problem explained:

  • If I go to the google chrome browser on the emulated device and enter “http://10.0.0.2:8005”, I get back a perfect response from the running server on my local host. So I know the emulated android app is not the problem.
  • For those unsure, http://10.0.0.2 is the loopback to the localhost or http://127.0.0.1 for emulated android devices.
  • If I then hit that same url from within my ionic app, I get “Failed to fetch”, “Unknown Error” and “null” back if I use the standard “js fetch()”, “Angular HttpClient”, and “Ionic native http” respectively. I tried all three methods and all have the similar result. :frowning:

Does anybody know why the ionic app that I created gets the above network errors when running in emulated android mode but not when being served? Yet the rest of the android device has access to the network? The most frustrating part of this is the lack of a real error, or problem. (I am not running any firewalls or proxies either).

I am running ionic 5.2.4, and the command I am using, to launch the app is:

> ionic cordova emulate android --livereload --consolelogs

In regards to the AVD, I have tried api 28 an 26, both have the same problem.

I have also done the following:

  • Installed the cordova white list pluggin
  • Set the cors, on/off,(client and server side)
  • Set <access origin="*"/> in the config.xml
  • Destroyed and re-created the ADV multiple times

Thanks to anyone in advance who has an idea.

I did find another thread with the same problem I am having… (google search works in mysterious ways… not sure why it took me this long to find it). But the problem was never resolved either.

The solution is to go to ~/resources/android/xml/network_security_config.xml in your ioinc app and add the 10.0.2.2 to the security config as below.

<?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-config>
</network-security-config>