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

前端 未结 11 1696
礼貌的吻别
礼貌的吻别 2020-11-22 02:03

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

相关标签:
11条回答
  • 2020-11-22 02:16

    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.

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

    0 讨论(0)
  • 2020-11-22 02:19

    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.

    0 讨论(0)
  • 2020-11-22 02:19

    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

    0 讨论(0)
  • 2020-11-22 02:21

    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.

    0 讨论(0)
  • 2020-11-22 02:22

    Use 10.0.2.2 for default AVD and 10.0.3.2 for genymotion.

    0 讨论(0)
  • 2020-11-22 02:22

    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.

    0 讨论(0)
提交回复
热议问题