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:09

its very simple , - GOTO command line (Window + R [type cmd]) - type ipconfig , that will show the current IP address of your PC - write taht IP address on your Android Phone's browser with :80 e.g (http://192.168.x.x:80)

Done

localhost will appear on your Phone

查看更多
笑指拈花
3楼-- · 2018-12-31 02:10

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.

enter image description here

查看更多
墨雨无痕
4楼-- · 2018-12-31 02:10

What worked for me ( target: debug an windows server app running on localhost:99999 from an client app running on android phone )

  1. phone and pc connected on the same network e.g. 192.168.1.xxx ( connected via wifi in my case)
  2. enable port 99999 in windows firewall
  3. run in cmd as administrator: netsh http add urlacl url=http://*:99999/ user=Everyone

then the service app url (my_dev_machine_ip:99999/path_to_service) was accessible from the phone.

查看更多
余欢
5楼-- · 2018-12-31 02:12

Personally I do not insist to use localhost when using device, there is no easy solution.

Just start the activity where you connect to localhost where you can use emulator. Whatever info you have to get from device can be generated with ease and can be sent to activity as parameter.

查看更多
栀子花@的思念
6楼-- · 2018-12-31 02:13

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.

查看更多
宁负流年不负卿
7楼-- · 2018-12-31 02:13

Use this in your ubuntu/Macbook to get the ip address of your system. Your mobile and system should be in the same network

ip addr | grep inet This will give you an ip address which looks like 192.168.168.46. Using this in your smartphone.

Hope this helps.

查看更多
登录 后发表回答