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

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

我错过了什么?


当前回答

另一件需要检查的事情是,当同时启用2.4G和5G且设备处于不同频率时,一些路由器在桥接请求方面存在问题。尝试禁用其中一个频率,以便两个设备连接到同一个接口。

其他回答

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

在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"/>

Ngrok是最好的解决方案。 如果你正在开发PHP,那么我建议安装Laravel Valet,它有MacOS和Linux版本,然后你可以使用Valet共享命令。如果你正在开发任何前端技术,需要共享一个端口,比如3000,那么直接使用ngrok


Xampp和ngrok

你也可以在Windows中使用ngrok和Xampp。您可以使用以下流程。这很简单。

在ngrok上注册一个免费账户 从下载部分下载ngrok文件并打开它

注意Apache服务器运行的端口。我的情况是80

在ngrok命令行中输入ngrok http YOUR_PORT并复制转发地址

现在你可以在任何设备上使用这个转发链接(即你的物理android设备),它将托管你的本地主机服务器。例如,

取代

http://localhost/my-files/

with

http://YOUR_FORWARDING_ADDRESS/my-files/

首先,把你的手机和电脑连接到普通的wifi。

然后,以管理员身份运行打开命令提示符

给出ipconfig命令

哪个显示无线局域网ip

在电话中使用ip:服务器的端口访问

其实很简单。

打开你的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防御防火墙” 检查应用程序是否启用了专用网络(应该有一个勾号) 如果未启用,请轻按“更改设置”,然后勾选应用程序“私有”下的复选框