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

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

我错过了什么?


当前回答

其实很简单。

打开你的Android手机/路由器的WiFi热点,连接你的 从笔记本电脑到手机 在本地主机启动服务器(我使用WAMP服务器的Windows) 现在打开命令提示符并输入

ipconfig

一旦你这样做了,你会看到如下内容:

Wireless LAN adapter Wireless Network Connection:
  Connection-specific DNS Suffix  . :
  Link-local IPv6 Address . . . . . : fe80::80bc:e378:19ab:e448%11
  IPv4 Address. . . . . . . . . . . : 192.168.43.76
  Subnet Mask . . . . . . . . . . . : 255.255.255.0
  Default Gateway . . . . . . . . . : 192.168.43.1

复制IPv4地址(在本例中为192.168.43.76) 在您的移动浏览器中,只需粘贴IPv4地址


注意:请将网络设置为“家庭网络”。将网络设置为家庭网络意味着您允许您的PC与同一网络上的其他设备共享内容。

如果你使用的是Windows 10,可以通过以下方法完成:

打开设置 去网络和互联网 在左侧菜单中选择WiFi 点击已连接WiFi的名称 配置网络的“网络配置文件”为“私有”

如果你遇到了问题,很可能是Windows防火墙的问题。

开放式控制面板 进入Windows防御防火墙 点击“允许一个应用程序或功能通过Windows防御防火墙” 检查应用程序是否启用了专用网络(应该有一个勾号) 如果未启用,请轻按“更改设置”,然后勾选应用程序“私有”下的复选框

其他回答

在创建应用程序时,使用移动设备调试或渲染是很有帮助的。可以使用带有端口号的IPv4地址通过手机访问系统localhost。需要注意的是,为了实现这一点,两台设备都应该连接到同一个wifi或网络,并且机器的网络应该是私有的。

在Windows PC上,您可能不需要做任何其他事情,只需使用“ipconfig”命令查找您的IPv4地址。

第一步:用USB线连接你的手机和电脑。 第二步:使用命令'ipconfig'找到你的IPv4地址(路由器的外部IP),例如192.168.1.5。 步骤3:只需从手机浏览器中访问“192.168.1.5:8080”。真的有用!

其他环境细节: Windows 7,谷歌Nexus 4 (4.2.2), Tomcat服务器,Grails应用程序。

你还应该确保你在AndroidManifest文件中有访问internet的权限:

<uses-permission android:name="android.permission.INTERNET"/>

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

首先,让你的机器(服务器运行的地方)IP地址是静态的。在android代码中输入此静态IP地址。 接下来进入你的wifi路由器接口,检查左边的面板。您将看到端口转发/转发等选项。点击它并转发端口80。 现在编辑httpd .conf文件,并将其编辑为

所有人允许

. 重新启动服务器。现在一切都可以正常工作了。

此外,您可以保留您的机器的IP地址,以便始终分配给您。 在路由器接口的左面板中,找到DHCP -> Address Reservation,单击它。输入计算机的MAC地址和您在代码中输入的IP地址。单击保存。这将为您的机器保留给定的IP地址。

在linux上使用ip addr而不是ifconfig,因为ifconfig已弃用多年,并且在最近的发行版中没有默认安装