ChatGPT解决这个技术问题 Extra ChatGPT

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

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, but the emulator still takes my request like a Google search for localhost or worse it says that it didn't found the page while my web server is normally running.

This question is about the Android Emulator and has solutions applicable to that case alone. If you face this problem when using a physical Android device, please see instead stackoverflow.com/questions/4779963/…

P
Primal Pappachan

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.

https://i.stack.imgur.com/m9eBE.png

You can read more from here.


why referring to teh other question? simply add the Link Android emulator Networking
Thanks for pointing the link out. I guess it was not there when the answer was posted.
@primpap :Can I use the machine name instead of the ip address??
@primpap this is not working with Mobile debugging. Is there some other ? Or i am doing mistake somewhere because 10.0.2.2 is working on emulator but not working when i connect mobile for usb debugging. ?
This solution only works on emulators, as asked in the specific question here. Those debugging on a physical Android device will have to use the server's actual IP address or name, they can't use this special emulator-only alias. This answer is good - but readers need to be careful that their situation is one in which it is applicable.
f
frogatto

//i.stack.imgur.com/6mhd8.png


So do we just add 10.0.3.2 in our host file?
P
Peter Mortensen

I used 10.0.2.2 successfully on my home machine, but at work, it did not work. After hours of fooling around, I created a new emulator instance using the Android Virtual Device (AVD) manager, and finally the 10.0.2.2 worked.

I don't know what was wrong with the other emulator instance (the platform was the same), but if you find 10.0.2.2 does not work, try creating a new emulator instance.


Unfortunatelly it doesn't work to me even after creating new AVD instance. What do you suggest? To reinstall everything?
I know this is old, but if you find that 10.0.2.2 is not working as the computer IP, follow these instructions to find it gtricks.com/android/how-to-find-ip-address-on-android
D
Danger14

Try http://10.0.2.2:8080/ where 8080 is your port number. It worked perfectly. If you just try 10.0.2.2 it won't work. You need to add port number to it. Also if Microsoft IIS has been installed try turning off that feature from control panel (if using any windows os) and then try as given above.


What if the pages are hosted on IIS?
@Marimba just figure out what port your IIS server serves on, then access 10.0.2.2:your_iis_port
this is a completed string and the only worked on my android 8.1 emulator
N
Nijat Ahmadli

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.


This is best option if you use ssl and openAuth.
Also best answer if you have CORS issues, thanks a lot!
P
Paul Butcher

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.


b
baybora.oren

For My Mac OS mountain Lion device :

http://10.0.2.2:8888

Works perfect !


H
Hugh Jeffner

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.

Installed Android Terminal Emulator from the app store Issue ip link show command to show all network interfaces. Of particular interest was the eth1 interface Issue ifconfig eth1 command, shows net as 172.17.100.15/255.255.255.0 Begin pinging addresses starting at 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 case

Maybe this can help someone else figure it out for other emulators.


m
mostafa3dmax

If you using Android Emulator :

You can connect to your Pc localhost by these IPs : 10.0.2.2:{port of your localhost} => if you set your machine port in xamp you must use that port . In my case 10.0.2.2:2080

https://i.stack.imgur.com/ZhU2d.jpg

https://i.stack.imgur.com/ttFEX.png

Also you can use your network adapter IP .In CMD write ipconfig and find your adapter ip address :

https://i.stack.imgur.com/kLwNK.png

https://i.stack.imgur.com/0U4L9.png

If emulator can not connect to this IPs close the emulator an open it by cold boot from AVD Manager :

https://i.stack.imgur.com/lga0n.png

If you using Genymotion :

You can connect to machine localhost by this IP : 10.0.3.2:{port number} Or your adapter IP address as I explained above: in my case : 192.168.1.3:2080


S
Serge Stroobandt

Allowing PWA installation

First of all, install the Android debug bridge:

$ sudo apt install adb android-sdk-platform-tools-common

Start your Android emulator as usual, e.g.:

$ ~/Android/Sdk/emulator/emulator -avd Pixel_3a_API_30_x86

Only then, configure a reverse proxy on the bridge of the Android emulator that will forward localhost HTTP requests to the appropriate port (e.g. 8000) of the localhost server running on your host computer and vice versa:

$ adb reverse tcp:8000 tcp:8000

A progressive web application (PWA) being served on localhost:8000 or 127.0.0.1:8000 will be installable and connect to its service-worker.js. Whereas PWA installation is not allowed from IP address 10.0.2.2.

Caveat: adb reverse tcp:8000 tcp:8000 needs to be reissued after each Android emulator evocation.

Hence, a bash script to launch an Android emulator, followed by a reverse proxy, would look like this:

#!/usr/bin/env bash

$HOME/Android/Sdk/emulator/emulator -avd Pixel_3a_API_30_x86 > /dev/null 2>&1

adb reverse tcp:8000 tcp:8000

R
Ricardo

If you are in windows you can go to simbol system and write ipconfig and check what ip is assigned to your machine.


This is not necessary when debugging from an emulator, as you can use the special alias for the host loopback interface cited in the other answers. However if debugging from a physical android device, then yes, you need to determine the actual address of your test server and use that.
T
Tim Bq

The accepted answer is correct, but didn't work in my case. I had to create the virtual device with the company VPN-client on the host machine turned off. This is quite understandable as many company networks use adresses starting with 10 (private network range), which could interfere with the special address 10.0.2.2


F
Fahad S. Ali

FOR ANYONE TRYING TO REACH A LOCAL IIS Server (ASP.NET)

For me, the accepted answer was not enough. I had to add a binding for 127.0.0.1 in the applicationhost.config, which was at the root of my ASP.NET solution.


E
Ed_

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


J
Jevgenij Kononov

I do not know, maybe this topic is already solved, but when I have tried recently do this on Windows machine, I have faced with lot of difficulties. So my solution was really simple. I have downloaded this soft http://www.lenzg.net/rinetd/rinetd.html followed their instructions about how to make port forwarding and then successfully my android device connected to make asp.net localhost project and stopped on my breaking point.

my rinetd.conf file:

10.1.1.20 1234 127.0.0.1 1234
10.1.1.20 82 127.0.0.1 82

Where 10.1.1.20 is my localhost ip, 82 and 1234 my ports Also I have craeted bath file for easy life yournameofbathfile.bat, put that file inside rinedfolder. My bath file:

rinetd.exe -c rinetd.conf

After starting this soft, start your aps.net server and try to access from android device or any device in your local network(for example Computer ABC starts putty) and you will see that everything works. No need to go to router setting or do any other complicated things. I hope this will help you. Enjoy.


There is nothing "simple" about your solution. Using 10.0.2.2 provided by Google for this purpose is simple. To know if a topic is solved there is a green check mark at the accepted answer. Which existed almost 6 years before you answered.
T
Tiago

I know this is old, but if you find that 10.0.2.2 is not working as the computer IP, follow these instructions to find it