无法在 Android 模拟器上访问本机的Web服务的解决办法
我在本地跑了一个 Tomcat ,我想在 Android 模拟器中直接通过下面的 url 地址访问 Tomcat 上的服务
http://192.168.0.20:8080/getweather
但是报错信息说 host is unresolved
我在模拟器上的浏览器上打开相同的地址,提示 page not found
请问我如何才能在模拟器上访问本机的web服务呢?
Android 模拟器是一个虚拟路由,其内部的网络跟你本机网络是不同的。
但这个路由是本机网络中的一部分,因此你可以通过 10.0.2.2 来访问本机网络服务
你上述的 URL 地址改成:http://10.0.2.2:8080/getweather 应该就好了。
Android 的文档中 Emulator Networking 有介绍到这个。
需要用 10.0.2.2 这个IP来访问本机网络
Emulator Networking
The emulator provides versatile networking capabilities that you can use to set up complex modeling and testing environments for your application. The sections below introduce the emulator's network architecture and capabilities.
Network Address Space
Each instance of the emulator runs behind a virtual router/firewall service that isolates it from your development machine's network interfaces and settings and from the internet. An emulated device can not see your development machine or other emulator instances on the network. Instead, it sees only that it is connected through Ethernet to a router/firewall.
The virtual router for each instance manages the 10.0.2/24 network address space — all addresses managed by the router are in the form of 10.0.2.<xx>, where <xx> is a number. Addresses within this space are pre-allocated by the emulator/router as follows:
Network Address | Description |
---|---|
10.0.2.1 | Router/gateway address |
10.0.2.2 | Special alias to your host loopback interface (i.e., 127.0.0.1 on your development machine) |
10.0.2.3 | First DNS server |
10.0.2.4 / 10.0.2.5 / 10.0.2.6 | Optional second, third and fourth DNS server (if any) |
10.0.2.15 | The emulated device's own network/ethernet interface |
127.0.0.1 | The emulated device's own loopback interface |
Note that the same address assignments are used by all running emulator instances. That means that if you have two instances running concurrently on your machine, each will have its own router and, behind that, each will have an IP address of 10.0.2.15. The instances are isolated by a router and cannot see each other on the same network. For information about how to let emulator instances communicate over TCP/UDP, see Connecting Emulator Instances.
Also note that the address 127.0.0.1 on your development machine corresponds to the emulator's own loopback interface. If you want to access services running on your development machine's loopback interface (a.k.a. 127.0.0.1 on your machine), you should use the special address 10.0.2.2 instead.
Finally, note that each emulated device's pre-allocated addresses are specific to the Android emulator and will probably be very different on real devices (which are also very likely to be NAT-ed, i.e., behind a router/firewall)
无法在 Android 模拟器上访问本机的Web服务的解决办法的更多相关文章
- Android模拟器报"Failed To Allocate memory 8"错误的解决办法
此错误是我们在允许AVD时,选择了默认的AVD插件所致. 解决方法:减少分配的内存大小.修改AVD的config.ini配置文件,将选项“hw.ramSize=1024”从1024改为256.
- Android 开发 关于APP无法安装到Android模拟器上的问题
我们在创建一个app后,打算安装到Android模拟器上.可能会出现无法安装,或者提示已经安装无法卸载的问题.这个时候需要添加兼容CPU. 选择 build.gradle 文件打开添加如下代码: an ...
- CentOS6.5(4)----宿主机无法访问虚拟机中的web服务解决方案
宿主机无法访问虚拟机中的web服务 在Windows7宿主机中的VMware虚拟机中安装了CentOS6.5操作系统,并且基于Nginx搭建了Web服务器,网页刚刚搭建好的时候,通过宿主机的浏览器可以 ...
- 5个Android开发中比较常见的内存泄漏问题及解决办法
android中一个对象已经不需要了,但是其他对象还持有他的引用,导致他不能回收,导致这个对象暂存在内存中,这样内存泄漏就出现了. 内存泄漏出现多了,会是应用占用过多的没存,当占用的内存超过了系统 ...
- XP系统中IIS访问无法显示网页,目前访问网站的用户过多。终极解决办法
无法显示网页 目前访问网站的用户过多. -------------------------------------------------------------------------------- ...
- Android中View类OnClickListener和DialogInterface类OnClickListener冲突解决办法
Android中View类OnClickListener和DialogInterface类OnClickListener冲突解决办法 如下面所示,同时导入这两个,会提示其中一个与另一个产生冲突. 1i ...
- QQ能上,但是网页打不开的解决办法
QQ能上,但是网页打不开,解决办法是:netsh winsock reset
- 虚拟机和宿主机不能互ping的解决办法等
1. 虚拟机和宿主机不能互ping的解决办法:禁用无关虚拟网卡. 2. 有时有效光驱设备为cdrom1. 3. CentOS 6.3 图形界面切换用户:System->Log Out
- Mac上安装第三方应用显示包资源破坏解决办法
Mac上安装第三方应用显示包资源破坏解决办法 步骤1:Spotlight搜索(快捷键:command+空格或右上角搜索的符号):搜索 “终端”步骤2:直接复制粘贴 sudo spctl --maste ...
随机推荐
- oracle误删除恢复
create table first_fill_20151207 as -- 生成到临时表select * from first_fillas of timestamp to_timestamp('2 ...
- 服务器bonding
server cat /etc/sysconfig/network-scripts/ifcfg-bond0 DEVICE=bond0 IPADDR=211.98.243.231 NETMASK=255 ...
- oracle11g空表不能导出记录
select 'alter table '||table_name||' allocate extent(size 64k);' from tabs t where not exists (selec ...
- JS产生四位随机数的方法
<script>var charactors="1234567890"; var value='',i; for(j=1;j<=4;j++){ i = parse ...
- leetcode 118
118. Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, ...
- Javascript 常用系统内置函数
1:在数组指定位置插入元素 array.splice(2, 0, "three"); //在索引2的位置,删除0个元素后,插入元素“three” 例子: // 原来的数组 va ...
- 逻辑回归的分布式实现 [Logistic Regression / Machine Learning / Spark ]
1- 问题提出 2- 逻辑回归 3- 理论推导 4- Python/Spark实现 # -*- coding: utf-8 -*- from pyspark import SparkContext f ...
- [Cocos2d-x for WP8学习笔记] HelloWorld结构分析
先来看一下目录结构: Assets:游戏资源文件,图片音频等,Resource文件夹也有类似功能 include:用于放置游戏头文件 Shaders:渲染器着色器文件(大雾) cocos2dorig. ...
- 三国塔防游戏android源码
三国塔防游戏源码,这个游戏源码比较完整的,因为上传有20M限制,把代码工程包分开了,主文件是TFGame,其他res大家按照安卓包加进去就行,欢迎下载并交流 ,大家可以参考一下吧.<ignore ...
- Android异常一、异步任务导致的窗口句柄泄漏问题(转)
05-05 10:36:41.009: E/WindowManager(4243): Activity com.tao.MyActivity has leaked window com.android ...