The Android emulator times out when calling a server on your own computer
Connection to http://10.0.2.2:8080 timed outShort 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.
Fix it fast
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/2Match what you see, then jump to its fix
- 1
The 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
10.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
The same build works in the emulator and times out on a physical device.The app runs on a real phone, not the emulatorFix - 4
The app targets Android 9 or higher and the URL starts with http://, not https://.Plain HTTP is blocked by the app's network rulesFix
- 1
What does the error look like?
Connection to http://10.0.2.2:8080 timed outthe 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:80803Verify
Open the URL in Chrome inside the emulator, then run the app again. Your server log should show the request.
adb reverse --listConfirmed by developer.android.com
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
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 devices2Fix
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:80803Verify
Open http://localhost:8080 in Chrome on the phone and see your server answer.
adb reverse --listConfirmed by developer.android.com
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.xml2Fix
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 installDebugConfirmed by developer.android.com
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
adb cannot start because something already holds its port
adb start-server failed
The adb server must bind local TCP port 5037, and every adb client talks to it there.
2 causes, each with a fixMobile apps
The phone shows as unauthorized and Android Studio will not run the app
adb devices lists the phone but Android Studio cannot deploy to it
An unauthorized phone means it has not trusted this computer's RSA key.
3 causes, each with a fixMobile 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 fixMobile apps
adb cannot connect to its daemon or start the server
* daemon started successfully *
The adb server is a background process on your computer that listens on TCP port 5037.
2 causes, each with a fixMobile apps
adb prints 'server version doesn't match this client; killing...'
adb server version doesn't match this client; killing...
adb runs a background server, and every adb command is a client.
3 causes, each with a fixMobile 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