How do you connect localhost in the Android emulator?
Thanks, @lampShaded for your answer.
In your API/URL directly use http://10.0.2.2:[your port]/
and under emulator setting add the proxy address as 10.0.2.2 with the port number. For more, you can visit: https://developer.android.com/studio/run/emulator-networking.html
Use 10.0.2.2
for default AVD and 10.0.3.2
for Genymotion
This is what finally worked for me.
- Backend running on localhost:8080
- Fetch your IP address (ipconfig on Windows)
Configure your Android emulator's proxy to use your IP address as host name and the port your backend is running on as port (in my case: 192.168.1.86:8080
Have your Android app send requests to the same URL (192.168.1.86:8080) (sending requests to localhost, and http://10.0.2.2 did not work for me)
Use 10.0.2.2
to access your actual machine.
As you've learned, when you use the emulator, localhost
(127.0.0.1
) refers to the device's own loopback service, not the one on your machine as you may expect.
You can use 10.0.2.2 to access your actual machine, it is an alias set up to help in development.