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

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

我错过了什么?


当前回答

这对我来说是有用的,我在127.0.0.1 ip后添加了另一行,以指定我想使用的设备的确切本地网络ip地址(而不是公共ip地址)。我用的是三星Galaxy S3

根据Bangptit的建议,编辑httpd.conf文件(x是版本号): C: \ wamp \ bin \ apache \ Apache2.x.x \ conf \ httpd . conf

搜索onlineoffline标签,并添加您的电话的ip(我发现我的电话ip地址在我的路由器配置页面):

Onlineoffline标签-不要删除

 Order Deny,Allow
 Deny from all
 Allow from 127.0.0.1

我的电话IP在下面一行

 Allow from 192.168.1.65 
 Allow from ::1
 Allow from localhost

可以将其扩展到包括整个子域,例如192.168.1.0/24等

其他回答

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

ip addr | grep inet这将给你一个看起来像192.168.168.46的ip地址。在你的智能手机上使用这个。

希望这能有所帮助。

试着转到这个文件: C: \ wamp \ bin \ apache \ Apache2.2.11 \ conf \ httpd . conf

#   onlineoffline tag - don't remove
    Order Deny,Allow
    Allow from all // change it Deny
    Allow from 127.0.0.1

并将10.0.2.2修改为您的IP地址。

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

如果您正在使用真实的设备,但仍然面临这个问题,请遵循以下步骤,因为这些步骤可以帮助我解决问题:

确保移动设备和笔记本电脑都在同一个WiFi网络上。 验证您的目标URL是否有端口号,例如:https://localhost:44301 在Android Studio终端运行此命令,adb reverse tcp:44301 tcp:44301 将44301替换为端口号

希望你能顺利地跑起来。

Happy codding

我个人不坚持在使用设备时使用本地主机,没有简单的解决方案。

只需启动您连接到本地主机的活动,在那里您可以使用模拟器。无论你从设备得到什么信息都可以轻松生成,并可以作为参数发送给活动。