When I was connecting my React Native app (running in the Android emulator) to my Spring Boot backend on my computer, using:
fetch("http://localhost:8080/api")
…didn’t work. ❌
But when I changed it to:
fetch("http://10.0.2.2:8080/api")
…it worked perfectly. ✅
Why?
- localhost inside the emulator points to the emulator itself — not your computer.
- Your Spring Boot server is running on your computer, not inside the emulator.
- 10.0.2.2 is a special IP address in the Android emulator that acts like “localhost of your computer”.
Think of it like this:
localhost → talks to the emulator
10.0.2.2 → talks to your computer
When to Use It
- Use 10.0.2.2 only for Android emulator.
- For iOS simulator, you can still use localhost.
- For real devices, use your computer’s local IP (e.g., 192.168.x.x).
Top comments (0)