How can I access my localhost from my Android device?

前端 未结 30 2256
面向向阳花
面向向阳花 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:21

    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)
      • on Linux the one-liner ifconfig | grep "inet " | grep -v 127.0.0.1 will yield only the important stuff
      • there's a bunch of suggestions on how to have a similar output on Windows
    • 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.

    NOTE: The ifconfig command has been deprecated and thus missing by default on Debian Linux, starting from Debian stretch. The new and recommended alternative for examining a network configuration on Debian Linux is ip command. For example to use ip command to display a network configuration run the following:

    ip address
    

    The above ip command can be abbreviated to:

    ip a
    

    If you still prefer to use ifconfig as part of your daily sys admin routine, you can easily install it as part of the net-tools package.

    apt-get install net-tools
    

    Reference is here

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

    The easier way to default localhost is to use http://localhost:port. This works in a laptop and Android as well.

    If it does not work, then in android set the default IP of your phone to 127.0.0.1:port :)

    Open terminal and type :-

     hostname -i
     #127.0.0.1
     hostname -I
     #198.168.*.*
    
    0 讨论(0)
  • 2020-11-21 06:22

    Try going to this file: C:\wamp\bin\apache\Apache2.2.11\conf\httpd.conf

    #   onlineoffline tag - don't remove
        Order Deny,Allow
        Allow from all // change it Deny
        Allow from 127.0.0.1
    

    And change 10.0.2.2 to your IP address.

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

    finally done in Ubuntu , i am running nodejs server on localhost:8080

    1) open terminal type ifconfig you will get ip something like this : inet addr:192.168.43.17

    2) now simply put url address like this : "192.168.43.17:8080" (8080 port coming from localhost port number) ex : "192.168.43.17:8080/fetch"

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

    Although one answer has been accepted but this worked for me:

    1. Make sure both your computer and android device are on same network.
    2. get your computer's ip address. (in case of mac type ifconfig inside terminal and look for en0/en1 -> inet it should be something like 19*.16*.1.4.) (on windows, use `ipconfig)
    3. Open your mobile settings and go to network-> long press your wifi network-> modify network -> scroll down to advanced options -> chose proxy manual and in proxy hostname type your address i.e. 19*.16*.1.4 inside port enter your port. and save it.

    4. search for what is my ip in google chrome, and get your ip, lets say it is 1**.1**.15*.2**

    5. try accessing 1**.1**.15*.2**:port/ from your mobile device.

    It is as simple as this.

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

    This is what worked for me, I added another line after the 127.0.0.1 ip to specify the exact local network ip address (not the public ip address) of the device I wanted to use. In my case my Samsung Galaxy S3

    As suggested by Bangptit edit the httpd.conf file (x being the version numbers): C:\wamp\bin\apache\Apache2.x.x\conf\httpd.conf

    Search for the onlineoffline tag and add the ip of your phone (I found my phones ip address in my routers configuration pages):

    onlineoffline tag - don't remove

     Order Deny,Allow
     Deny from all
     Allow from 127.0.0.1
    

    my phones ip in the line below

     Allow from 192.168.1.65 
     Allow from ::1
     Allow from localhost
    

    One could extend this to include an entire sub domain too for e.g. 192.168.1.0/24 etc

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