我可以使用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.

其他回答

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

在终端:

ifconfig

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

基于笔记本电脑的移动设备连接wamp服务器的解决方案:

首先,wifi不是路由器。因此,为了将我的移动设备连接到我笔记本电脑上基于localhost的wamp服务器,我需要一个路由器。 我下载并安装了一个免费的虚拟路由器:https://virtualrouter.codeplex.com/

配置它真的很简单:

右键单击系统托盘中的虚拟路由器图标 2 .单击“配置虚拟路由器” 填写密码 如果您的internet连接是以太网,请选择共享连接:以太网 然后在你的笔记本电脑和设备上打开wifi 在您的设备上连接到虚拟路由器网络名

然后你可以通过你的设备连接到你的笔记本电脑,启动浏览器并填写你的笔记本电脑的IPV4地址 (要在windows上找到它,键入CMD: ipconfig,并找到ipv4地址)

您应该看到wamp服务器主页。

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

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

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

首先,把你的手机和电脑连接到普通的wifi。

然后,以管理员身份运行打开命令提示符

给出ipconfig命令

哪个显示无线局域网ip

在电话中使用ip:服务器的端口访问

Mac OS X用户

我通过启用远程管理实现了这一点:

确保你的手机和笔记本电脑连接到同一个WiFi网络 在Mac上,进入系统首选项/共享 开启远程管理

你会看到一条类似这样的消息:

其他用户可以使用some.url.com地址管理您的计算机

在你的Android设备上,你现在应该能够访问some.url.com,它委托给Mac上的localhost。你也可以使用ifconfig来获得Mac的IP地址。


ngrok的可移植解决方案(任何带有Node.js的操作系统)

如果你不介意用临时域名公开你的项目,你可以使用ngrok。假设我有一个在localhost:9460上运行的应用程序,我可以简单地编写

npm install ngrok -g

ngrok http 9460

这将给我:

Session Status                online
Update                        update available (version 2.2.8, Ctrl-U to update)
Version                       2.2.3
Region                        United States (us)
Web Interface                 http://127.0.0.1:4040
Forwarding                    http://f7c23d14.ngrok.io -> localhost:9460
Forwarding                    https://f7c23d14.ngrok.io -> localhost:9460

Connections                   ttl     opn     rt1     rt5     p50     p90
                              0       0       0.00    0.00    0.00    0.00

我现在可以通过https://f7c23d14.ngrok.io远程查看localhost。这是与客户分享设计工作或进展的好机会。


替代解决方案与nginx代理通过

如果你通过nginx proxy_pass运行类似这样的东西,它将需要更多的调整-这是一个hack方法,但它适用于我,我对改进它的建议是开放的:

启用远程管理(如上所述) 临时将服务器设置为监听81端口,而不是80端口 输入以下命令:

sudo nginx -s reload

访问http://youripaddress: 81

server {
  listen 80;
  listen 81;   # <-------- add this to expose the app on a unique port
  server_name  ~^(local|local\.m).example.com$;
  # ...
}

重新加载并访问http://youripaddress:81