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

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

我错过了什么?


当前回答

使用几个不同的本地主机服务器时遇到了这个问题。通过使用Python简单服务器,我的应用程序在几秒钟内就启动并在手机上运行了。它只需要几秒钟,所以在进入任何更复杂的解决方案之前值得一试。首先,确保安装了Python。cmd+r并输入python for Windows或在mac终端输入$ python——version。

下一个:

cd <your project root>

$ python -m SimpleHTTPServer 8000

然后在网络上找到你的主机地址,我使用mac上的系统首选项/共享来找到它。点击它到你的Android设备,应该加载你的index.html,你应该是好的。

如果不是,那么问题是其他的,你可能想看看其他建议的解决方案。好运!

*编辑*

如果你正在使用Chrome浏览器,另一个快速的解决方案是Chrome扩展的Web服务器。我发现这是一种快速和超级简单的方法来访问我的手机上的localhost。只要确保在选项下检查可访问本地网络,它应该在你的手机上工作没有任何问题。

其他回答

Windows解决方案:

你无法浏览你的网站,主要是因为你的防火墙( 默认操作系统防火墙或防病毒防火墙)正在阻止传入 连接。

Windows防火墙下:

要允许特定端口上的传入连接,请转到:

控制面板> Windows防御防火墙>高级设置>右侧 单击“入站规则>添加新规则”。 然后: 选择“端口”单选按钮。 输入要在其上启用传入连接的特定端口/范围(服务器正在运行的端口) 选择“允许连接”单选按钮。 单击Next(除非您想更改默认值) 给规则起个名字,然后单击Finish。

注意:

如果您正在使用带有防火墙的防病毒软件,那么上述步骤将不起作用,因为防病毒软件将禁用Windows防火墙,而是运行自己的防火墙。

在这种情况下,根据防病毒程序,转到您的防病毒防火墙设置并打开端口。

其实很简单。

打开你的Android手机/路由器的WiFi热点,连接你的 从笔记本电脑到手机 在本地主机启动服务器(我使用WAMP服务器的Windows) 现在打开命令提示符并输入

ipconfig

一旦你这样做了,你会看到如下内容:

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

复制IPv4地址(在本例中为192.168.43.76) 在您的移动浏览器中,只需粘贴IPv4地址


注意:请将网络设置为“家庭网络”。将网络设置为家庭网络意味着您允许您的PC与同一网络上的其他设备共享内容。

如果你使用的是Windows 10,可以通过以下方法完成:

打开设置 去网络和互联网 在左侧菜单中选择WiFi 点击已连接WiFi的名称 配置网络的“网络配置文件”为“私有”

如果你遇到了问题,很可能是Windows防火墙的问题。

开放式控制面板 进入Windows防御防火墙 点击“允许一个应用程序或功能通过Windows防御防火墙” 检查应用程序是否启用了专用网络(应该有一个勾号) 如果未启用,请轻按“更改设置”,然后勾选应用程序“私有”下的复选框

这些回答中没有一个提到从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.

试着转到这个文件: 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

并将10.0.2.2修改为您的IP地址。

“Chrome for Android的端口转发让你更容易在手机上测试你的开发站点。它的工作原理是在移动设备上创建一个监听TCP端口,该端口映射到开发机器上的特定TCP端口。这些端口之间的流量通过USB传输,因此连接不依赖于你的网络配置。”

更多详情请访问:https://developer.chrome.com/devtools/docs/remote-debugging#port-forwarding