我可以使用Android模拟器访问我的笔记本电脑web服务器,我使用10.0.2.2:portno 工作得很好。
但当我连接真正的Android手机时,手机浏览器无法连接到我笔记本电脑上的同一个web服务器。手机和笔记本电脑通过USB线连接。如果我运行 adb devices命令,我可以看到我的手机。
我错过了什么?
我可以使用Android模拟器访问我的笔记本电脑web服务器,我使用10.0.2.2:portno 工作得很好。
但当我连接真正的Android手机时,手机浏览器无法连接到我笔记本电脑上的同一个web服务器。手机和笔记本电脑通过USB线连接。如果我运行 adb devices命令,我可以看到我的手机。
我错过了什么?
当前回答
简单。 首先,让你的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.
因为这是一个老问题,有一个新的方法来做这个非常非常简单。下载ADB Chrome扩展,然后按照这些说明:
https://developers.google.com/chrome-developer-tools/docs/remote-debugging
以管理员身份运行CMD
在CMD屏幕上输入ipconfig,屏幕将显示文本
就像这张照片
您可以使用这个IP访问您的本地主机 您必须连接到与您的PC连接的相同网络
在Ilya Saunkin回答的基础上,这里有一个命令,应该输出你的IP地址格式为超链接-只需用3000代替你的端口号:
ifconfig | grep "inet " | grep -v 127.0.0.1 | sed 's/netmask.*//g' | sed 's/inet//g' | awk '{print "http://"$1":3000/"}'
根据您运行命令的位置,您应该能够(在Mac上)按住命令键并单击/双击打开链接。
“Chrome for Android的端口转发让你更容易在手机上测试你的开发站点。它的工作原理是在移动设备上创建一个监听TCP端口,该端口映射到开发机器上的特定TCP端口。这些端口之间的流量通过USB传输,因此连接不依赖于你的网络配置。”
更多详情请访问:https://developer.chrome.com/devtools/docs/remote-debugging#port-forwarding