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

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

我错过了什么?


当前回答

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

其他回答

最简单的方法(这对我来说完美无缺)是在0.0.0.0:<port_no>本地托管您的站点,并使用移动设备访问它,在浏览器中使用<local_ipv4_address>:<port_no>/<path>。

要知道您的本地ipv4地址,只需在cmd中键入ipconfig 任何连接到同一网络的设备都可以访问这个url。

您可以让web服务器在环回接口上侦听,而不是在网络接口上侦听。主要表现为:

在127.0.0.1和localhost(从localhost或Android模拟器)工作 点击192.168.xxx。xxx不能工作,无论是从本地主机,局域网或广域网

我在这里的回答中讨论了更多关于诊断和修复的问题。

其实很简单。

打开你的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防御防火墙” 检查应用程序是否启用了专用网络(应该有一个勾号) 如果未启用,请轻按“更改设置”,然后勾选应用程序“私有”下的复选框

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

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

打开终端并输入:-

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

Ngrok是最好的解决方案。 如果你正在开发PHP,那么我建议安装Laravel Valet,它有MacOS和Linux版本,然后你可以使用Valet共享命令。如果你正在开发任何前端技术,需要共享一个端口,比如3000,那么直接使用ngrok


Xampp和ngrok

你也可以在Windows中使用ngrok和Xampp。您可以使用以下流程。这很简单。

在ngrok上注册一个免费账户 从下载部分下载ngrok文件并打开它

注意Apache服务器运行的端口。我的情况是80

在ngrok命令行中输入ngrok http YOUR_PORT并复制转发地址

现在你可以在任何设备上使用这个转发链接(即你的物理android设备),它将托管你的本地主机服务器。例如,

取代

http://localhost/my-files/

with

http://YOUR_FORWARDING_ADDRESS/my-files/