Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to localhost from Android Studio emulator

I am trying to test web application in Android 2.3.3 browser. Connection using http://10.0.2.2 results "web page is unavailable". Nothing found in Android Studio documentation.

Does anyone run into the same problem? How did you solve it?

like image 594
Firanolfind Avatar asked Mar 20 '17 13:03

Firanolfind


People also ask

What is the localhost for Android emulator?

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.

How do I connect to localhost?

To access the server from itself, use http://localhost/ or http://127.0.0.1/ . To access the server from a separate computer on the same network, use http://192.168.X.X where X.X is your server's local IP address. You can find the sever's local IP address (assuming it's Linux) by running hostname -I . 127.0.

What is localhost in Android Studio?

Each instance of the Android emulator runs behind a virtual router/firewall. You can find a list of IP addresses used in the network address space here. Among these IP addresses, you will find a special alias to your host computer's loopback interface (aka localhost). This alias is 10.0.


2 Answers

You're going to need to connect to your computer's IP.

If you're on Windows open CMD and type ipconfig this will give you your local IP.

If you're on Linux or OSX open terminal and use the ifconfig command.

Since the emulator is a full Android device it has its own network and doesn't use the same hosts file that your machine has. Along with its own IP.

Edit: Grammar fix.

like image 193
ryan.wise Avatar answered Sep 27 '22 16:09

ryan.wise


I use my local ip for that i.e. 192.168.0.1 and it works. Verify your local IP.

like image 23
Thiago Neves Avatar answered Sep 27 '22 18:09

Thiago Neves