如何从Android设备访问本地主机?
我可以使用Android emulator访问我的笔记本电脑web服务器,我使用的是如何从Android设备访问本地主机?,android,Android,我可以使用Android emulator访问我的笔记本电脑web服务器,我使用的是10.0.2.2:portno 效果很好 但当我连接真正的Android手机时,手机浏览器无法连接到笔记本电脑上的同一个web服务器。手机通过USB电缆连接到笔记本电脑。如果我运行 adb设备命令,我可以看到我的手机 我缺少什么?USB不能为移动设备提供网络 如果您的桌面和手机都连接到同一WiFi(或任何其他本地网络),则使用路由器分配的桌面IP地址(非localhost和非127.0.0.1) 要查找桌面的IP
10.0.2.2:portno
效果很好
但当我连接真正的Android手机时,手机浏览器无法连接到笔记本电脑上的同一个web服务器。手机通过USB电缆连接到笔记本电脑。如果我运行
adb设备命令,我可以看到我的手机
我缺少什么?USB不能为移动设备提供网络 如果您的桌面和手机都连接到同一WiFi(或任何其他本地网络),则使用路由器分配的桌面IP地址(非
localhost
和非127.0.0.1
)
要查找桌面的IP地址,请执行以下操作:
- 在命令行中键入
(Windows)或ipconfig
(Unix)ifconfig
- 在Linux上,一行程序
只会产生重要的内容ifconfig | grep“inet”| grep-v 127.0.0.1
- 有一个关于如何在Windows上有类似输出的问题
- 在Linux上,一行程序
- 将会有一堆IP的
- 请全部试用(前面提到的
和localhost
除外)127.0.0.1
- 首先找出您的路由器外部IP地址()
- 然后,在路由器上,将一些端口转发到
:
- 最后使用外部IP地址和转发端口
ifconfig
命令已被弃用,因此默认情况下已丢失。在Debian Linux上检查网络配置的新的和推荐的替代方法是ip命令。例如,要使用ip命令显示网络配置,请运行以下命令:
ip address
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
上述ip命令可以缩写为:
ip a
如果您仍然喜欢使用ifconfig
作为日常系统管理例行程序的一部分,您可以轻松地将其作为nettools
软件包的一部分进行安装
apt-get install net-tools
参考是但是有一个更好的解决方案。您可以使用IP地址“10.0.2.2”访问主机。Android团队就是这样设计的。因此,您的Web服务器可以在localhost上完美运行,您可以通过Android应用程序访问它”http://10.0.2.2:8080“我找到了这个问题的快速解决方案。试试这个。它应该能帮助你解决这个问题 我只改变了一件事,教程中说您将“127.0.0.1”更改为“全部”,改为您的服务器正在运行的IP地址 之后,您应该能够连接到本地主机
以下是链接页面中信息的(校对)副本: 第一步 安装Wamp服务器(或您喜欢的任何其他服务器) 这是我所知道的设置本地服务器最好的服务器之一。如果已安装Apache或任何其他服务器,请忽略此步骤 从这里下载并安装Wamp服务器 步骤2 为Windows防火墙中的端口80添加新规则
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
ipconfig
命令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地址。实际上非常简单
- 打开Android手机/路由器的WiFi热点并连接 将笔记本电脑连接到手机
- 在
上启动您的服务器(我正在使用用于Windows的WAMP服务器)localhost
- 现在打开命令提示符并输入
ip address
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
无线LAN适配器无线网络连接:
特定于连接的DNS后缀:
链接本地IPv6地址…:fe80::80bc:e378:19ab:e448%11
IPv4地址………:192.168.43.76
子网掩码………:255.255.255.0
默认网关………:192.168.43.1
- 复制IPv4地址(在本例中,它是
)192.168.43.76
- 在移动浏览器中,只需粘贴IPv4
Session Status online
Update update available (version 2.2.8, Ctrl-U to update)
Version 2.2.3
Region United States (us)
Web Interface http://127.0.0.1:4040
Forwarding http://f7c23d14.ngrok.io -> localhost:9460
Forwarding https://f7c23d14.ngrok.io -> localhost:9460
Connections ttl opn rt1 rt5 p50 p90
0 0 0.00 0.00 0.00 0.00
server { listen 80; listen 81; # <-------- add this to expose the app on a unique port server_name ~^(local|local\.m).example.com$; # ... }
Order Deny,Allow Deny from all Allow from 127.0.0.1
Allow from 192.168.1.65 Allow from ::1 Allow from localhost
cd <your project root> $ python -m SimpleHTTPServer 8000
brew cask install ngrok ngrok http 4000
Forwarding http://4cc5ac02.ngrok.io -> localhost:4000
hostname -i #127.0.0.1 hostname -I #198.168.*.*
Happy codding