How can I access my localhost from my Android device?

I'm able to access my laptop web server using the Android emulator, I'm using 10.0.2.2:portno works well.

But when I connect my real Android phone, the phone browser can't connect to the same web server on my laptop. The phone is connected to the laptop using a USB cable. If I run the adb devices command, I can see my phone.

What am I missing?


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)
  • 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.


    It is very simple.

    Turn on Wifi Hotspot of your Android phone/router and connect your Laptop to your phone.

    Start your server at localhost (I am using wamp server)

    Now open command prompt and enter ipconfig command you will get following things

    Wireless LAN adapter Wireless Network Connection:
      Connection-specific DNS Suffix  . :
      Link-local IPv6 Address . . . . . : fe80::80bc:e378:19ab:e448%11
      IPv4 Address. . . . . . . . . . . : **192.168.43.76**
      Subnet Mask . . . . . . . . . . . : 255.255.255.0
      Default Gateway . . . . . . . . . : 192.168.43.1
    

    Copy this 192.168.43.76 in your mobile browser.

    Note : Please set your network as "Home Network". Setting Home Network means allowing your PC to share stuff with other devices on the same network. If your using Windows 10 go to WiFi > Network properties of current network > Make this PC Discoverable.


    mac osx users

    i had success by enabling remote management:

  • ensure your phone and laptop are connected to the same wifi network
  • on mac, go to system preferences/sharing
  • enable remote management
  • You will see a message similar to this

    Other users can manage your computer using the address some.url.com

    On your android device you should now be able to hit some.url.com , which delegates to localhost on your mac. You can also use ifconfig to get the ip of your macbook


    portable solution with ngrok (any OS with node or npm)

    If you don't mind exposing your project with a temporary domain you can use ngrok . Lets say I have an app that runs on localhost:9460

    npm install ngrok -g
    ngrok http 9460
    

    This will give me:

    Session Status                online
    Update                        update available (version 2.2.8, Ctrl-U to update)
    Version                       2.2.3
    Region                        United States (us)
    Web Interface                 http://127.0.0.1:4040
    Forwarding                    http://f7c23d14.ngrok.io -> localhost:9460
    Forwarding                    https://f7c23d14.ngrok.io -> localhost:9460
    
    Connections                   ttl     opn     rt1     rt5     p50     p90
                                  0       0       0.00    0.00    0.00    0.00
    

    I can now reach https://f7c23d14.ngrok.io as a way to remotely view localhost. This is great to share design work or progress with clients


    alternate solution with nginx proxy pass

    If you are running something like this through nginx proxy_pass it will require a bit more tweaking - this is a hacky approach, but it works for me and i am open to suggestions on improving it:

  • enable remote management (as mentioned above)
  • temporarily set the server to listen on port 81 as opposed to 80
  • sudo nginx -s reload
  • visit http://youripaddress:81
  • ```

    server {
      listen 80;
      listen 81; # <-------- add this to expose the app on unique port
      server_name  ~^(local|local.m).example.com$;
      ...
    }
    

    reload and visit http://youripaddress:81

    链接地址: http://www.djcxy.com/p/23688.html

    上一篇: Android Studio中的“无法解析符号R”

    下一篇: 我如何从我的Android设备访问我的本地主机?