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

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

我错过了什么?


当前回答

使用USB线:

(例如,如果使用WAMP服务器):

1)在你的电脑上安装你的Android驱动程序,下载便携式Android Tethering反向工具,并通过反向工具应用程序连接你的Android设备。

2)点击WAMP图标>上线(重启后)。

3)在Android浏览器中打开你的IP(即http://192.168.1.22或http://164.92.124.42)

要找到您的本地IP地址,单击“开始>运行>cmd并输入ipconfig,您的IP地址将显示在输出中。

这是所有。现在你可以从Android访问(打开)localhost。

其他回答

Windows解决方案:

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

Windows防火墙下:

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

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

注意:

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

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

在你的ubuntu/Macbook中使用这个来获取你系统的ip地址。你的手机和系统应该在同一个网络中

ip addr | grep inet这将给你一个看起来像192.168.168.46的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

简单。 首先,让你的android设备和电脑连接在同一个网络上,比如路由器 用windows+R打开命令提示符,搜索cmd,然后打开。在命令中输入ipconfig,获取ipv4地址。

注意:防火墙会阻止你的电脑通过网络访问,所以你需要关闭网络的防火墙,无论是公共的还是私有的。

如何关闭防火墙 打开控制面板>系统和安全> windows防火墙>在左侧窗格选择打开和关闭windows防火墙。>然后选择“关闭windows防火墙”(不推荐)

你完成了

然后打开你的移动设备,运行你的IP地址192.168.1.xxx


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