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

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

我错过了什么?


当前回答

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

我只改变了一件事,在教程中,你把“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访问您的本地主机。

其他回答

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

所有人允许

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

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

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

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.

事情就是这么简单。

Ngrok允许你把本地主机放到临时服务器上,设置非常简单。我在这里提供了一些可以在链接中找到的步骤:

从上面的链接下载ngrok压缩包 拉开拉链 在本地运行服务器并记录端口号 在终端中,进入ngrok所在的文件夹,输入ngrok http[端口号]

您将在终端中看到一个小仪表板,其中有一个指向本地主机的地址。将你的应用指向这个地址,然后构建到你的设备上。

使用netconfig xml并在manifest.xml中分配它是最好的解决方法。这将绕过android默认的https限制。

以上的方法对我不起作用。Mac是这样的:

在终端:

ifconfig

然后把inet后面的数字放到手机浏览器里: