我可以使用Android模拟器访问我的笔记本电脑web服务器,我使用10.0.2.2:portno 工作得很好。
但当我连接真正的Android手机时,手机浏览器无法连接到我笔记本电脑上的同一个web服务器。手机和笔记本电脑通过USB线连接。如果我运行 adb devices命令,我可以看到我的手机。
我错过了什么?
我可以使用Android模拟器访问我的笔记本电脑web服务器,我使用10.0.2.2:portno 工作得很好。
但当我连接真正的Android手机时,手机浏览器无法连接到我笔记本电脑上的同一个web服务器。手机和笔记本电脑通过USB线连接。如果我运行 adb devices命令,我可以看到我的手机。
我错过了什么?
当前回答
然而,有一个更好的解决方案。您可以使用IP地址“10.0.2.2”访问您的主机。Android团队就是这样设计的。所以你的web服务器可以完美地运行在本地主机和从你的Android应用程序,你可以通过“http://10.0.2.2:8080”访问它。
其他回答
在mac上,如果你的服务器是8080,在终端上运行这个命令
echo "
rdr pass inet proto tcp from any to any port 8080 -> 127.0.0.1 port 8080
" | sudo pfctl -ef -
使用connectify和xampp或等效,并在移动URL栏上输入ip地址访问
如果您正在使用真实的设备,但仍然面临这个问题,请遵循以下步骤,因为这些步骤可以帮助我解决问题:
确保移动设备和笔记本电脑都在同一个WiFi网络上。 验证您的目标URL是否有端口号,例如:https://localhost:44301 在Android Studio终端运行此命令,adb reverse tcp:44301 tcp:44301 将44301替换为端口号
希望你能顺利地跑起来。
Happy codding
使用netconfig xml并在manifest.xml中分配它是最好的解决方法。这将绕过android默认的https限制。
这些回答中没有一个提到从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.