我可以使用Android模拟器访问我的笔记本电脑web服务器,我使用10.0.2.2:portno 工作得很好。
但当我连接真正的Android手机时,手机浏览器无法连接到我笔记本电脑上的同一个web服务器。手机和笔记本电脑通过USB线连接。如果我运行 adb devices命令,我可以看到我的手机。
我错过了什么?
我可以使用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.
其他回答
默认localhost的更简单方法是使用http://localhost:port。这也适用于笔记本电脑和安卓系统。
如果它不工作,然后在android设置您的手机的默认IP 127.0.0.1:端口:)
打开终端并输入:-
hostname -i
#127.0.0.1
hostname -I
#198.168.*.*
使用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。
最后在Ubuntu中完成,我在localhost:8080上运行nodejs服务器
1)打开终端类型ifconfig 你将得到类似这样的IP: inet addr:192.168.43.17
2)现在简单地把url地址像这样: "192.168.43.17:8080"(8080端口来自本地主机端口号) 例如:“192.168.43.17:8080/fetch”
您可以让web服务器在环回接口上侦听,而不是在网络接口上侦听。主要表现为:
在127.0.0.1和localhost(从localhost或Android模拟器)工作 点击192.168.xxx。xxx不能工作,无论是从本地主机,局域网或广域网
我在这里的回答中讨论了更多关于诊断和修复的问题。
即使您的移动设备和电脑通过usb连接,此解决方案也可用:
此时需要使用端口转发功能。 在谷歌chrome inspect窗口(chrome://inspect)可以看到连接的设备。
点击端口fowading按钮,并设置一个随机端口,例如3000到您的笔记本电脑使用的端口,例如8080。
现在从设备上使用localhost:3000来访问笔记本电脑中的localhost:8080(or_whatever_ip:portno)。 您可以在移动浏览器中查看。在移动浏览器中尝试localhost:3000。 确保勾选“端口转发设置”窗口中的“启用端口转发”复选框