How can I access my localhost from my Android device?

前端 未结 30 2272
面向向阳花
面向向阳花 2020-11-21 05:53

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, th

相关标签:
30条回答
  • 2020-11-21 06:37

    There is however a far better solution. You can access your host machine with the IP address "10.0.2.2". This has been designed in this way by the Android team. So your webserver can perfectly run at localhost and from your Android app you can access it via "http://10.0.2.2:8080".

    0 讨论(0)
  • 2020-11-21 06:39

    ngrok lets you put your localhost onto a temporary server and is very simple to set up. I've provided some steps here that can be found in the link:

    1. Download the ngrok zip from the link above
    2. Open the zip
    3. Run your server locally and take note of the port number
    4. In the terminal, go to the folder where ngrok lives and type ngrok http [port number]

    You'll see a little dashboard in your terminal with an address pointing to your localhost. Point your app to that address and build to your device.

    0 讨论(0)
  • 2020-11-21 06:40

    Was running into this problem using several different localhost servers. Finally got my app up and running on the phone in seconds just by using the Python simple server. It only takes a few seconds so is worth a try before getting into any more complicated solutions. First, make sure you have Python installed. cmd+r and type python for Windows or $ python --version in mac terminal.

    Next:

    cd <your project root>
    
    $ python -m SimpleHTTPServer 8000
    

    Then just find the address of your host machine on the network, I used System Preferences/Sharing on mac to find it. Tap that into your Android device and should load your index.html and you should be good.

    If not then the problem is something else and you may want to look into some of the other suggested solutions. Good luck!

    * EDIT *

    Another quick solution to try if you're using Chrome is the Web Server for Chrome extension. I found it a quick and super easy way to get access to localhost on my phone. Just make sure to check Accessible to local network under the Options and it should work on your cell without any problem.

    0 讨论(0)
  • 2020-11-21 06:41

    First of all make your machine(where server is running)IP address static. Enter this static IP address in the android code. Next go to your wifi router's interface and check the left panel. You will see option such as port forwarding/forwarding. Click on it and forward port 80. Now edit your htttpd.conf file and edit it for

    Allow from All

    . Restart your server. Everything should work fine now.

    Additionally you can reserve the IP address of your machine so that it is always allocated to you. In the left panel of your router's interface, find DHCP -> Address Reservation, click on it. Enter your machine's MAC address and the IP address you have entered in the code. Click on save. This will reserve the given IP address for your machine.

    0 讨论(0)
  • 2020-11-21 06:42

    Another thing to check is that some routers have issues bridging the requests when both 2.4G and 5G are enabled and the devices are on different frequencies. Trying disabling one of the frequencies so both devices are connected to the same interface.

    0 讨论(0)
  • 2020-11-21 06:43

    "Port forwarding on Chrome for Android makes it easy to test your development site on mobile. It works by creating a listening TCP port on your mobile device that maps to a particular TCP port on your development machine. Traffic between these ports travels through USB, so the connection doesn't depend on your network configuration."

    More details here: https://developer.chrome.com/devtools/docs/remote-debugging#port-forwarding

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