Maaz Zindani

Loading the index…

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

The Android emulator times out when calling a server on your own computer

ErrorConnection to http://10.0.2.2:8080 timed out

Short answer

Inside the emulator, 127.0.0.1 and localhost point to the emulated phone, not to your computer. Your computer's loopback is reachable at 10.0.2.2, or at localhost after adb reverse. If the call still times out, a firewall on the computer may block it, or the app may be on a real phone, where 10.0.2.2 does not exist.

  • Applies to Android Emulator, Android 14, Android 15, Android Studio
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the exact URL the app calls. If it contains localhost or 127.0.0.1, go to the first cause. If it already uses 10.0.2.2, confirm the server answers on your computer first.

    curl -I http://127.0.0.1:8080/
    grep -rn "localhost\|127.0.0.1\|10.0.2.2" app/src/main/
  2. 2Match what you see, then jump to its fix

    1. 1The base URL in the app is http://localhost:... or http://127.0.0.1:..., and the request never reaches your server log.The app calls localhost or 127.0.0.1Fix
    2. 210.0.2.2 is in the URL and the server runs, but every call still ends in a timeout.A firewall on your computer or network blocks the connectionFix
    3. 3The same build works in the emulator and times out on a physical device.The app runs on a real phone, not the emulatorFix
    4. 4The app targets Android 9 or higher and the URL starts with http://, not https://.Plain HTTP is blocked by the app's network rulesFix

What does the error look like?

  • Connection to http://10.0.2.2:8080 timed out
  • the same URL works in the browser on your computer but not in the app

Cause 1 of 4

The app calls localhost or 127.0.0.1

1Check

The emulator keeps its own loopback address. 127.0.0.1 inside the emulator is the emulated device, not your development machine.

grep -rn "localhost\|127.0.0.1" app/src/main/

2Fix

Use the special alias 10.0.2.2, which the emulator maps to the loopback interface of your computer. Or keep localhost in the app and forward the port with adb reverse.

// Retrofit base URL for the emulator
.baseUrl("http://10.0.2.2:8080/")

# or keep http://localhost:8080/ in the app and run, once per emulator start:
adb reverse tcp:8080 tcp:8080

3Verify

Open the URL in Chrome inside the emulator, then run the app again. Your server log should show the request.

adb reverse --list

Confirmed by developer.android.com

Did this fix it?

Cause 2 of 4

A firewall on your computer or network blocks the connection

1Check

The emulator acts like a normal app on your computer, so a local firewall program or a firewall on the network can block it. Test with the firewall rule for your server port switched off for a minute.

curl -I http://127.0.0.1:8080/

2Fix

Allow inbound connections for the server process or port in your firewall, then retry.

# Example only: macOS and Windows use their own firewall settings.
# Make sure the server process is allowed to accept connections on the port.

3Verify

Retry the call from the app and watch the server log.

curl -I http://127.0.0.1:8080/

Confirmed by developer.android.com

Did this fix it?

Cause 3 of 4

The app runs on a real phone, not the emulator

1Check

The pre-allocated addresses, including 10.0.2.2, are specific to the Android Emulator and are likely to be different on real devices.

adb devices

2Fix

On a real device, use your computer's address on the shared Wi-Fi network, or run adb reverse over USB so localhost works on the phone.

adb reverse tcp:8080 tcp:8080

3Verify

Open http://localhost:8080 in Chrome on the phone and see your server answer.

adb reverse --list

Confirmed by developer.android.com

Did this fix it?

Cause 4 of 4

Plain HTTP is blocked by the app's network rules

1Check

For apps that target Android 9 (API 28) and higher, cleartext HTTP is off by default.

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

2Fix

For development, allow cleartext only for the host you test with, using a network security config file.

<!-- res/xml/network_security_config.xml -->
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">10.0.2.2</domain>
    </domain-config>
</network-security-config>

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

3Verify

Rebuild, install and repeat the request.

./gradlew installDebug

Confirmed by developer.android.com

Did this fix it?

How do I stop it coming back?

  • Keep the base URL in a build config field per build type, so emulator, device and production never share one hard-coded address.
  • Prefer adb reverse when you also test in a WebView, because 10.0.2.2 is not a secure context there.
  • Start the server first and test it with curl before blaming the app.

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

Mobile apps

Android Studio cannot pair the phone over Wi-Fi

The Pair devices over Wi-Fi dialog lists no device

Wireless pairing needs the phone on Android 11 or higher and on the same Wi-Fi network as the computer, with current Platform-Tools.

4 causes, each with a fix

Mobile apps

adb tcpip 5555 fails because no device is found

adb: more than one device/emulator

adb tcpip only works while the device is attached and listed by adb, because it tells the phone to start listening on port 5555.

4 causes, each with a fix

Next step

Want this handled for good?

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