What can I do in the Android emulator to connect it to my localhost web server page at http://localhost
or http://127.0.0.1
?
I\'ve tried it
I needed to figure out the system host IP address for the emulator "Nox App Player". Here is how I figured out it was 172.17.100.2
.
ip link show
command to show all network interfaces. Of particular interest was the eth1 interfaceifconfig eth1
command, shows net as 172.17.100.15/255.255.255.0
172.17.100.1
, got a hit on `172.17.100.2'. Not sure if a firewall would interfere but it didn't in my caseMaybe this can help someone else figure it out for other emulators.
The localhost refers to the device on which the code is running, in this case the emulator.
If you want to refer to the computer which is running the Android simulator, use the IP address 10.0.2.2 instead.
You can read more from here.
Another workaround is to get a free domain from no-ip.org and point it to your local ip address.
Then, instead of using http://localhost/yourwebservice
you can try http://yourdomain.no-ip.org/yourwebservice
according to documentation:
10.0.2.2 - Special alias to your host loopback interface (i.e., 127.0.0.1 on your development machine)
check Emulator Networking for more tricks on emulator networking.
Use 10.0.2.2
for default AVD and 10.0.3.2
for genymotion.
You can actually use localhost:8000
to connect to your machine's localhost by running below command each time when you run your emulator (tested on Mac only):
adb reverse tcp:8000 tcp:8000
Just put it to Android Studio terminal.
It basically sets up a reverse proxy in which a http server running on your phone accepts connections on a port and wires them to your computer or vice versa.