How can I access my localhost from my Android device?

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

    First of all connect your phone and computer to common wifi.

    Then, open command prompt using run as administrator

    Give ipconfig command

    Which shows wireless lan ip

    Use ip:port of your server to access in phone

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

    Adding a solution for future developers.

    Copy address of your ip address. right click on your network -> network and sharing-> click on the connection you currently have-> details-> then the address beside ipv4 address is your ip address, note this down somewhere

    Go to control panel -> system and security -> windows firewall -> advanced settings -> inbound rules -> new rules (follow the steps to add a port e.g 80, its really simple to follow)

    put your ip address that you noted down on your phone browser and the port number you created the rule for beside it. e.g 192.168.0.2:80 and wala.

    Possible solution if it doesn't connect. right click network->open network and sharing-> look under view your active connections, under the name of your connection at the type of connection and click on it if it is public, and make sure to change it to a home network.

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

    I found a quick solution to this problem. Try this link. It should help you fix the problem.

    I only changed one thing, where the tutorial states you change '127.0.0.1' to 'All', change it to the IP address your server is running on instead.

    After that, you should be able to connect to your localhost.


    Below is a (proofread) copy of the information from the linked page:

    Step 1

    Install the Wamp server (or any other you prefer).

    This is one of the best servers I know to set up a local server. If you have installed Apache or any other server, ignore this step.

    Download and install Wamp Server from here.

    Step 2

    Add a new rule for port 80 in Windows Firewall.

    1. Open the Control Panel and select Windows Firewall.

    2. Select Advanced settings from the left panel of the Windows Firewall setting page.

    3. Select Inbound Rules from the left panel, then select New Rule.

    4. Select Port and click Next.

    5. Select the “Specific local ports” radio button and enter 80 as the port value.

    6. Keep Allow the connection unchanged and move to the next step.

    7. Keep Profile options unchanged and click Next.

    8. Give some nice name to your new rule and click Finish.

    This will enable port 80 access on local network IP.

    Step 3

    Edit the httpd.conf file of the Wamp server to fix the 403 error.

    We need to edit this file. Otherwise, we will get the 403 forbidden error when we access the localhost through a local network IP.

    1. Click on the Wamp server tray icon.

    2. Open Apache server sub menu.

    3. Select httpd.conf.

    4. Find this section of configuration in the httpd.conf file:

      Directory “c:/wamp/www/”
      #
      # Possible values for the Options directive are “None”, “All”,
      # or any combination of:
      # Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
      #
      # Note that “MultiViews” must be named *explicitly* — “Options All”
      # doesn’t give it to you.
      #
      # The Options directive is both complicated and important. Please see
      # http://httpd.apache.org/docs/2.2/mod/core.html#options
      # for more information.
      #
      Options Indexes FollowSymLinks
      
      #
      # AllowOverride controls what directives may be placed in .htaccess files.
      # It can be “All”, “None”, or any combination of the keywords:
      # Options FileInfo AuthConfig Limit
      #
      AllowOverride all
      
      #
      # Controls who can get stuff from this server.
      #
      
      # onlineoffline tag – don’t remove
      Order Deny,Allow
      Deny from all
      Allow from 127.0.0.1
      

    Find and replace ‘127.0.0.1’ with ‘All’, save the file, and restart your Wamp server.

    Step 4

    Find your local network IP.

    1. Open the command prompt.

    2. Type and enter the ipconfig command.

    3. In my case, my local area network address is 10.0.0.2.

    This is the IP which you need to access your localhost on your Android phone over wifi. To test if it is working, type this IP address in your desktop browser where your localhost server is installed. The browser should display your localhost page successfully. This will assure that this local network IP is now successfully accessible on your Android phone.

    I hope this tutorial will help you to access your localhost over wifi.

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

    Simple. First and foremost make your your android device and computer is connected on thee same network.e.g router Open command prompt by windows+R and search for cmd then open. On the command type ipconfig and get the ipv4 address.

    NB: Firewall blocks access of your computer along network so you need to turn off firewall for the network if either public or private.

    How to turn off firewall Open control panel > System and security > windows firewall > on the left pane select turn on and off windows firewall. > Then select turn off windows firewall(not recommended)

    You are done

    then open your mobile device and run your ip address 192.168.1.xxx


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

    Mac OS X users

    I achieved this by enabling remote management:

    • Ensure that 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 go to some.url.com, which delegates to localhost on your Mac. You can also use ifconfig to get the IP address of your Mac.


    Portable solution with ngrok (any OS with Node.js)

    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 I can simply write

    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
    • Type in the following command:
    sudo nginx -s reload
    
    • Visit http://youripaddress:81
    server {
      listen 80;
      listen 81;   # <-------- add this to expose the app on a unique port
      server_name  ~^(local|local\.m).example.com$;
      # ...
    }
    

    Reload and visit http://youripaddress:81

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

    This solution is usable when your mobile device and computer is not in same network:

    You need to use port forwarding in this case. In the Google chrome inspect window (chrome://inspect) You can see the connected devices.

    Click on the port fowading button, and set a random port say 3000 to the port which your laptop uses say 8080.

    now use localhost:3000 from device to access the localhost:8080(or_whatever_ip:portno) in the laptop. You can check it in the mobile browser. Try localhost:3000 in the mobile browser. Make sure to check the "Enable port forwarding" checkbox in the "Port forwarding settings" window

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