android emulator
localhost connection
web server
development
troubleshooting

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:

bash
python3 -m http.server 8080

then the emulator should use:

text
http://10.0.2.2:8080

not:

text
http://localhost:8080

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.

kotlin
1val retrofit = Retrofit.Builder()
2    .baseUrl("http://10.0.2.2:8080/")
3    .addConverterFactory(GsonConverterFactory.create())
4    .build()

For a quick request with HttpURLConnection:

kotlin
1import java.net.HttpURLConnection
2import java.net.URL
3
4val url = URL("http://10.0.2.2:8080/health")
5val connection = url.openConnection() as HttpURLConnection
6connection.requestMethod = "GET"
7
8println(connection.responseCode)

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:

python
1from flask import Flask
2
3app = Flask(__name__)
4
5@app.get("/health")
6def health():
7    return {"status": "ok"}
8
9app.run(host="0.0.0.0", port=8080)

Then test the endpoint from the host first:

bash
curl http://127.0.0.1:8080/health

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:

xml
<application
    android:usesCleartextTraffic="true" />

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:port from the emulator fails because localhost refers 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.2 on 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 curl or a browser.

Summary

  • In the Android emulator, use 10.0.2.2 instead of localhost to 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.

Course illustration
Course illustration

All Rights Reserved.