Accessing Localhost:Port from Android Emulator

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

Sample Image

How can I access my localhost from my Android device?

USB doesn't provide network to mobile device.

If both your desktop and phone are connected to the same WiFi (or any other local network), then use your desktop IP address assigned by the router (not localhost and not 127.0.0.1).

To find out the IP address of your desktop:

  • type into the command line ipconfig (Windows) or ifconfig (Unix)

    • on Linux the one-liner ifconfig | grep "inet " | grep -v 127.0.0.1 will yield only the important stuff
    • there's a bunch of suggestions on how to have a similar output on Windows
  • there's going to be a bunch of IP's
  • try all of them (except the forementioned localhost and 127.0.0.1)

If your phone is connected to the mobile network, then things are going to be harder.

Either go hardcore:

  • first find out your router external IP address (https://www.google.de/search?q=myip)
  • then, on the router, forward some port to <your desktop IP>:<server port number>
  • finally use the external IP address and forwarded port

Otherwise use something like xip.io or ngrok.

NOTE: The ifconfig command has been deprecated and thus missing by default on Debian Linux, starting from Debian stretch. The new and recommended alternative for examining a network configuration on Debian Linux is ip command. For example to use ip command to display a network configuration run the following:

ip address

The above ip command can be abbreviated to:

ip a

If you still prefer to use ifconfig as part of your daily sys admin routine, you can easily install it as part of the net-tools package.

apt-get install net-tools

Reference is here

How to access localhost from android emulator?

localhost refers to your local machine. It's a common mistake that people think that the Android emulator (or iOS for that matter) is also part of localhost since it is running on the same machine. But this isn't the case.

The emulators are running as a device in your device and mimic the working as if it was a physical device. It has it's own IP address and cannot reach your localhost loopback address. Your app runs as a application on another machine, namely te emulator. It gets a bit confusing when using UWP, since this does run as an application on your local machine.

That is why you would have to use the network address of your machine where the server application is hosted. This address typically starts with 192.168.x.x or 10.x.x.x and can be found in the network settings of your machine.

It seems that you already discovered this. When using the 10.0.2.2 address you receive a HTTP 400. Which means something in your request wasn't right, but you can conclude from this that the server application can actually be reached. But the content of the request causes a problem in your server app. Since you do not provide any code for the /Account/Register endpoint it is impossible to tell what is going on there. Put a breakpoint in your server code, retry the request and try to see why a HTTP 400 is triggered.

How to connect to the localhost of an Android emulator?

You can do that with adb forward. See port forwarding

adb forward tcp:3000 tcp:3000

Then on your computer you can connect to http://localhost:3000 and it will be routed to port 3000 of the emulator.

How to access localhost from on android emulator inside a VM?

Ok, the problem was that when I ran the Web service on ISS Express from Visual Studio it wasn't running on my local IP address 127.0.0.1:[MY WEB API PORT].

I found this out thanks to @JasonYe-MSFT who told me about the command

netstat -ant

When I ran the command I found that the port that my web app was running on wasn't attached to my local IP instead it was formatted like so

[::]:64339

So in visual studio instead of running the web app using IIS Express, i changed it too the project itself and when i ran the command again, the port now had my local IP address connected to it.

Inside the android emulator, i can now load the web api using AVD default IP address '10.0.2.2'. So the full address is

10.0.2.2:"MY WEB API PORT"/swagger

How to connect to my http://localhost web server from Android Emulator

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.

Sample Image

You can read more from here.

How to access localhost from a NOX app player android emulator?

I found the IP to be 172.17.100.2. Found this out by installing the Android "Fing" app to browse the network and tried the first result. Works great. Not sure why NOX didn't stick with the usual 10.0.2.2 like most other emulators I've used.



Related Topics



Leave a reply



Submit