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

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

我错过了什么?


当前回答

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

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

希望你能顺利地跑起来。

Happy codding

其他回答

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

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

希望这能有所帮助。

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

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

我找到了一个快速解决这个问题的方法。试试这个链接。它应该能帮你解决问题。

我只改变了一件事,在教程中,你把“127.0.0.1”改为“所有”,把它改为你的服务器正在运行的IP地址。

在此之后,您应该能够连接到本地主机。


以下是链接页面信息的(校对)副本:

步骤1

安装Wamp服务器(或其他您喜欢的服务器)。

这是我所知道的设置本地服务器最好的服务器之一。如果您已经安装了Apache或其他服务器,请忽略此步骤。

从这里下载并安装Wamp服务器。

步骤2

在Windows防火墙中为80端口添加一条新规则。

打开控制面板,选择“Windows防火墙”。 在“Windows防火墙”设置页面的左侧面板中选择“高级设置”。 从左侧面板中选择Inbound Rules,然后选择New Rule。 选择“Port”,单击“Next”。 选择“Specific local ports”单选按钮,输入80作为端口值。 保持保持连接不变,执行下一步。 保持Profile选项不变并单击Next。 给新规则起个好听的名字,然后单击Finish。

这将启用本地网络IP上的80端口访问。

步骤3

编辑Wamp服务器的httpd.conf文件以修复403错误。

我们需要编辑这个文件。否则,当我们通过本地网络IP访问本地主机时,将得到403禁止错误。

Click on the Wamp server tray icon. Open Apache server sub menu. Select httpd.conf. Find this section of configuration in the httpd.conf file: Directory “c:/wamp/www/” # # Possible values for the Options directive are “None”, “All”, # or any combination of: # Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews # # Note that “MultiViews” must be named *explicitly* — “Options All” # doesn’t give it to you. # # The Options directive is both complicated and important. Please see # http://httpd.apache.org/docs/2.2/mod/core.html#options # for more information. # Options Indexes FollowSymLinks # # AllowOverride controls what directives may be placed in .htaccess files. # It can be “All”, “None”, or any combination of the keywords: # Options FileInfo AuthConfig Limit # AllowOverride all # # Controls who can get stuff from this server. # # onlineoffline tag – don’t remove Order Deny,Allow Deny from all Allow from 127.0.0.1

找到并将' 127.0.0.1 '替换为' All ',保存文件,并重新启动Wamp服务器。

步骤4

找到您的本地网络IP。

打开命令提示符。 键入并输入ipconfig命令。 在我的例子中,我的局域网地址是10.0.0.2。

这是你需要通过wifi访问你的Android手机上的本地主机的IP。要测试它是否正常工作,请在安装本地主机服务器的桌面浏览器中键入此IP地址。浏览器应该成功地显示您的本地主机页面。这将确保这个本地网络IP现在可以在您的Android手机上成功访问。

我希望本教程将帮助您通过wifi访问您的本地主机。

如果你在Linux上运行 这对我很有用

sudo iptables -F

虽然有一个答案已经被接受,但这对我来说很管用:

Make sure both your computer and android device are on same network. get your computer's ip address. (in case of mac type ifconfig inside terminal and look for en0/en1 -> inet it should be something like 19*.16*.1.4.) (on windows, use `ipconfig) Open your mobile settings and go to network-> long press your wifi network-> modify network -> scroll down to advanced options -> chose proxy manual and in proxy hostname type your address i.e. 19*.16*.1.4 inside port enter your port. and save it. search for what is my ip in google chrome, and get your ip, lets say it is 1**.1**.15*.2** try accessing 1**.1**.15*.2**:port/ from your mobile device.

事情就是这么简单。