28

Started a local server in my computer with IP 127.0.0.1 listening on port 3000.

However, I am unable to access it via my android app when I run the emulator on android studio. I got a timeout error from my app's networking library, so it is not making connection.

XYseven
  • 475
  • 1
  • 4
  • 11

2 Answers2

44

Use the IP address 10.0.2.2. You can read more at here.

dub
  • 1,396
  • 12
  • 22
Gurjit Singh
  • 1,060
  • 2
  • 13
  • 16
6

On each Android emulator Android Studio runs a router for it in order to isolate the emulated device from your development system. Thus in order to access a local server running on your development system (your laptop, desktop, etc) you must use the special IP of 10.0.2.2 and the port the server is listening on. To learn more read this page.

JWhit
  • 61
  • 1
  • 1
  • 3
    Where should I enter `10.0.2.2`? In the emulator device, my local machine? If it is the local machine, which interface? – testing Jan 08 '19 at 12:52
  • For future visitors: Where you put this IP address depends on why you are trying to connect. For example, if you use OkHttp to make a HTTP request to a HTTP server running on the same machine as the Android emulator, you put `10.0.2.2` in the URL string in your code. If you need further help, you should post a new question with your code, details about what you are trying to do, and a link to this question. – Code-Apprentice Apr 03 '22 at 03:01