Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

Axios request works on iOS or in a browser but fails on Android with Network Error

ErrorAxiosError: Network Error

Short answer

A React Native app has no CORS, so on Android a Network Error usually means the request never reached the server. Inside the emulator localhost is the emulator itself, so use 10.0.2.2. Since API 28 plain http is blocked unless you allow it. A phone must use your computer's LAN address.

  • Applies to React Native CLI template, axios 1, Android 9 and later, Android Emulator
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the exact URL the app calls, then open that same URL from where the app runs: the emulator browser, or the phone's browser. If the page does not load there, the cause is networking, not Axios.

    // in the failing call
    console.log(axios.defaults.baseURL);
  2. 2Match what you see, then jump to its fix

    1. 1The baseURL is http://localhost:3000 or http://127.0.0.1:3000 and only Android failslocalhost points at the Android emulator, not at your computerFix
    2. 2The URL starts with http:// and the same call to an https:// URL worksPlain http:// is blocked: cleartext traffic is off by default since Android 9Fix
    3. 3Works in the emulator with 10.0.2.2, fails on the phoneA real phone cannot reach the address you useFix

What does the error look like?

  • AxiosError: Network Error

Cause 1 of 3

localhost points at the Android emulator, not at your computer

1Check

Android's emulator docs say 127.0.0.1 is the emulated device's own loopback. Your computer's loopback is reached with the special address 10.0.2.2.

grep -rn "localhost\|127.0.0.1" src/

2Fix

Use 10.0.2.2 for the emulator, or forward the port from the emulator to your computer with adb reverse and keep localhost.

// emulator only
const baseURL = 'http://10.0.2.2:3000';
# or keep localhost and forward the port:
adb reverse tcp:3000 tcp:3000

3Verify

The same request now returns data in the emulator.

adb devices

Confirmed by developer.android.com

Did this fix it?

Cause 2 of 3

Plain http:// is blocked: cleartext traffic is off by default since Android 9

1Check

React Native's network guide says cleartext traffic is blocked by default as of API level 28. Android's network security guide says the same and shows how to allow it.

grep -n "usesCleartextTraffic\|networkSecurityConfig" android/app/src/main/AndroidManifest.xml

2Fix

Best fix: serve the API over https. For development only, allow cleartext for your dev host in a network security config and point the manifest at it.

<!-- android/app/src/main/res/xml/network_security_config.xml -->
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">10.0.2.2</domain>
    </domain-config>
</network-security-config>

<!-- AndroidManifest.xml, on the <application> tag -->
android:networkSecurityConfig="@xml/network_security_config"

3Verify

Rebuild the app (a manifest change needs a native build) and repeat the request.

npx react-native run-android

Confirmed by developer.android.com

Did this fix it?

Cause 3 of 3

A real phone cannot reach the address you use

1Check

10.0.2.2 exists only inside the emulator. A phone needs an address it can reach: your computer's LAN address on the same Wi-Fi, or a USB port forward.

# computer LAN address: macOS System Settings > Network, Windows ipconfig
adb devices

2Fix

Point the baseURL at your computer's LAN address, with phone and computer on the same Wi-Fi. Or forward the port over USB with adb reverse and use localhost.

const baseURL = 'http://192.168.1.20:3000'; // your computer's LAN address
# or over USB:
adb reverse tcp:3000 tcp:3000

3Verify

Open the same URL in the phone's browser first, then run the app.

adb devices

Confirmed by reactnative.dev

Did this fix it?

How do I stop it coming back?

  • Keep the API base URL in one config file and set it per target: emulator, phone, production.
  • Use https for every environment except a local dev server.
  • Log error.code and error.message in the catch block so a Network Error is not all you see.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.