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

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

我错过了什么?


当前回答

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

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

其他回答

使用connectify和xampp或等效,并在移动URL栏上输入ip地址访问

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

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.

事情就是这么简单。

USB不为移动设备提供网络。

如果你的桌面和手机都连接到同一个WiFi(或任何其他本地网络),那么使用路由器分配的桌面IP地址(不是localhost,也不是127.0.0.1)。

要找出您的桌面的IP地址:

在命令行输入ipconfig (Windows)或ifconfig (Unix) 在Linux上,一行程序ifconfig | grep "inet " | grep -v 127.0.0.1将只产生重要的东西 有很多关于如何在Windows上有类似输出的建议 会有一大堆IP 尝试所有这些(除了前面提到的localhost和127.0.0.1)

如果你的手机连接到移动网络,那么事情就会变得更加困难。

要么走硬核路线:

首先找出你的路由器外部IP地址(https://www.google.de/search?q=myip) 然后,在路由器上转发一些端口到<您的桌面IP>:<服务器端口号> 最后使用外部IP地址和转发端口

否则使用类似xip的东西。IO或ngrok。

注意:ifconfig命令已经弃用,因此在Debian Linux上默认没有,从Debian扩展开始。在Debian Linux上检查网络配置的新的推荐替代方法是ip命令。例如,使用ip命令显示网络配置,执行如下命令:

ip address

以上ip命令可以缩写为:

ip a

如果您仍然喜欢使用ifconfig作为日常系统管理程序的一部分,您可以轻松地将其作为net-tools包的一部分安装。

apt-get install net-tools

参考资料在这里

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

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

打开终端并输入:-

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

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

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

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