我可以使用Android模拟器访问我的笔记本电脑web服务器,我使用10.0.2.2:portno 工作得很好。

但当我连接真正的Android手机时,手机浏览器无法连接到我笔记本电脑上的同一个web服务器。手机和笔记本电脑通过USB线连接。如果我运行 adb devices命令,我可以看到我的手机。

我错过了什么?


当前回答

以上的方法对我不起作用。Mac是这样的:

在终端:

ifconfig

然后把inet后面的数字放到手机浏览器里:

其他回答

这些回答中没有一个提到从Windows防御防火墙打开Apache HTTP服务器。使用XAMPP v. 5.6.40,您可以尝试以下解决方案:

Go to Windows Defender Firewall, click on Inbound rules and look for Apache HTTP Server. There will be two instances of this rule. Double click on it and set the Action to Allow the connection. Go to the Advanced tab and tick Domain, Private, and Public options from the Profiles section. Go to the Protocols and Ports tab and configure your local ports. By default, XAMPP server runs on port 80. If you're using multiple ports for web development, simply place commas after each port. Save your changes. Run cmd and type ipconfig. Take note of your IP Address. Restart Apache from your XAMPP Control Panel. Try accessing your app via address:port/path, where address is your IP address, port is your port (80 in most cases), and path to your project in /xampp/htdocs.

在linux上使用ip addr而不是ifconfig,因为ifconfig已弃用多年,并且在最近的发行版中没有默认安装

我的工作(目标:调试运行在localhost:99999上的windows服务器应用程序,从运行在android手机上的客户端应用程序)

电话和电脑连接在同一网络上,例如192.168.1。XXX(在我的情况下通过wifi连接) 在Windows防火墙中开启99999端口 以管理员身份运行cmd: netsh http add urlacl url=http://*:99999/ user=Everyone

然后,可以从手机访问服务应用程序url (my_dev_machine_ip:99999/path_to_service)。

默认localhost的更简单方法是使用http://localhost:port。这也适用于笔记本电脑和安卓系统。

如果它不工作,然后在android设置您的手机的默认IP 127.0.0.1:端口:)

打开终端并输入:-

 hostname -i
 #127.0.0.1
 hostname -I
 #198.168.*.*

虽然有一个答案已经被接受,但这对我来说很管用:

Make sure both your computer and android device are on same network. 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) 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. search for what is my ip in google chrome, and get your ip, lets say it is 1**.1**.15*.2** try accessing 1**.1**.15*.2**:port/ from your mobile device.

事情就是这么简单。