How can I access my localhost from my Android devi

2018-12-31 01:45发布

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?

标签: android
30条回答
闭嘴吧你
2楼-- · 2018-12-31 02:25

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.*.*
查看更多
有味是清欢
3楼-- · 2018-12-31 02:25

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.

查看更多
美炸的是我
4楼-- · 2018-12-31 02:26

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.

查看更多
忆尘夕之涩
5楼-- · 2018-12-31 02:26

A solution to connect my mobile device to my wamp server based on my laptop:

First, wifi is not a router. So to connect my mobile device to my wamp server based on localhost on my laptop, I need a router. I have downloaded and installed a free virtual router: https://virtualrouter.codeplex.com/

Configuring it is really simple:

  1. right click on virtual router icon in System Tray
  2. click on Configure virtual router
  3. fill a password
  4. if your internet connection is in ethernet, choose Shared connection : Ethernet
  5. Then set wifi on on your laptop and device
  6. On your device connect to the virtual router network name

Then you can connect to your laptop via your device by launching a browser and fill the IPV4 address of your laptop (to find it on windows, type in cmd : ipconfig, and find ipv4 address)

You should see your wamp server home page.

查看更多
倾城一夜雪
6楼-- · 2018-12-31 02:28

On Windows PC You may not need to do anything else than finding out your IPv4 Address using "ipconfig" command.

  • Step 1 : Connect your phone to PC using USB cable.
  • Step 2 : Use command 'ipconfig' to find out your IPv4 Address (External IP of the Router) e.g 192.168.1.5 in my case.
  • Step 3: Just access "192.168.1.5:8080" from your phone browser. It works !

Other Env Details : Windows 7, Google Nexus 4 (4.2.2), Tomcat Server, Grails Application.

You should also make sure that you have the permission to access internet in AndroidManifest file :

<uses-permission android:name="android.permission.INTERNET"/>
查看更多
荒废的爱情
7楼-- · 2018-12-31 02:28

As this is an old question, there is a new way to do this that is really really simple. Download the ADB Chrome Extension and then follow these instructions:

https://developers.google.com/chrome-developer-tools/docs/remote-debugging

查看更多
登录 后发表回答