How to connect to my http//localhost web server from Android Emulator
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When code runs inside the Android emulator, localhost points to the emulator itself, not to your development machine. To reach a web server running on your computer, you usually need the special host alias 10.0.2.2 and a server that is listening on the right interface.
Use 10.0.2.2 Instead of localhost
The stock Android emulator maps 10.0.2.2 to the host computer's loopback interface. That means a server running on your machine at port 8080 is normally reachable from the emulator at http://10.0.2.2:8080.
For example, if your local API is running here on the host:
then the emulator should use:
not:
Inside the emulator, localhost means "this virtual device," so requests never leave the emulator process.
Point Your Android Client at the Host Alias
If your app uses Retrofit or another HTTP client, set the base URL to 10.0.2.2 during local emulator development.
For a quick request with HttpURLConnection:
This is enough for local API testing as long as the server is running and the app is allowed to make cleartext HTTP requests.
Make Sure the Server Is Reachable
Your host application still has to accept incoming connections. Some development servers bind only to 127.0.0.1; others bind to all interfaces with 0.0.0.0. For emulator use, the safest practice is to confirm the server is actively listening on the port you expect.
For example, with a simple Flask app:
Then test the endpoint from the host first:
If the host cannot reach the server, the emulator will not reach it either.
Watch for Android Cleartext Restrictions
On newer Android versions, HTTP traffic may be blocked by default. If you are connecting to a local development server over plain HTTP rather than HTTPS, you may need to allow cleartext traffic.
One simple manifest-level option is:
That is acceptable for local development builds, but you should not carry it into a production configuration without a reasoned security decision.
Emulator Variants and Real Devices
10.0.2.2 is the standard alias for the Android emulator bundled with Android Studio. Other environments may differ. For example, Genymotion commonly uses 10.0.3.2 for the host machine. A physical device on Wi-Fi is different again: it must connect to your computer's LAN IP address, not an emulator alias.
Common Pitfalls
- Using
http://localhost:portfrom the emulator fails becauselocalhostrefers to the emulator, not the host machine. - Forgetting Android cleartext restrictions can produce connection errors even when the server is reachable. Allow HTTP traffic for local development if needed.
- Testing against
10.0.2.2on a physical device does not work. Use your computer's LAN IP address for real hardware. - Assuming every emulator uses the same host alias is risky. Stock Android emulator uses
10.0.2.2, while some alternatives use a different mapping. - Starting the server without confirming it is actually listening on the expected port leads to false debugging paths. Verify the host service first with
curlor a browser.
Summary
- In the Android emulator, use
10.0.2.2instead oflocalhostto reach the host machine. - Point your HTTP client to the host alias and the correct development port.
- Confirm the local server is running and reachable before debugging the app.
- Allow cleartext HTTP for local development when your API is not using HTTPS.
- Treat emulator URLs and physical-device URLs as different environments.

