我在本地跑了一个 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服务的解决办法的更多相关文章

  1. Android模拟器报"Failed To Allocate memory 8"错误的解决办法

    此错误是我们在允许AVD时,选择了默认的AVD插件所致. 解决方法:减少分配的内存大小.修改AVD的config.ini配置文件,将选项“hw.ramSize=1024”从1024改为256.

  2. Android 开发 关于APP无法安装到Android模拟器上的问题

    我们在创建一个app后,打算安装到Android模拟器上.可能会出现无法安装,或者提示已经安装无法卸载的问题.这个时候需要添加兼容CPU. 选择 build.gradle 文件打开添加如下代码: an ...

  3. CentOS6.5(4)----宿主机无法访问虚拟机中的web服务解决方案

    宿主机无法访问虚拟机中的web服务 在Windows7宿主机中的VMware虚拟机中安装了CentOS6.5操作系统,并且基于Nginx搭建了Web服务器,网页刚刚搭建好的时候,通过宿主机的浏览器可以 ...

  4. 5个Android开发中比较常见的内存泄漏问题及解决办法

    android中一个对象已经不需要了,但是其他对象还持有他的引用,导致他不能回收,导致这个对象暂存在内存中,这样内存泄漏就出现了.   内存泄漏出现多了,会是应用占用过多的没存,当占用的内存超过了系统 ...

  5. XP系统中IIS访问无法显示网页,目前访问网站的用户过多。终极解决办法

    无法显示网页 目前访问网站的用户过多. -------------------------------------------------------------------------------- ...

  6. Android中View类OnClickListener和DialogInterface类OnClickListener冲突解决办法

    Android中View类OnClickListener和DialogInterface类OnClickListener冲突解决办法 如下面所示,同时导入这两个,会提示其中一个与另一个产生冲突. 1i ...

  7. QQ能上,但是网页打不开的解决办法

    QQ能上,但是网页打不开,解决办法是:netsh winsock reset

  8. 虚拟机和宿主机不能互ping的解决办法等

    1. 虚拟机和宿主机不能互ping的解决办法:禁用无关虚拟网卡. 2. 有时有效光驱设备为cdrom1. 3. CentOS 6.3 图形界面切换用户:System->Log Out

  9. Mac上安装第三方应用显示包资源破坏解决办法

    Mac上安装第三方应用显示包资源破坏解决办法 步骤1:Spotlight搜索(快捷键:command+空格或右上角搜索的符号):搜索 “终端”步骤2:直接复制粘贴 sudo spctl --maste ...

随机推荐

  1. oracle创建job权限

    grant create job to ja_ods; grant manage scheduler to ja_ods;

  2. javascript计算两个时间差

    其实,javascript计算时间差的方式非常简单,如果是默认的Date()类型,直接相减就是相差的毫秒数. var d1 = new Date('2016/03/28 10:17:22'); var ...

  3. 多行文字垂直居中(完美兼容chrome firefox IE6 7 8 9)

    在说到这个问题的时候,也许有人会问CSS中不是有vertical-align属性来设置垂直居中的吗?即使是某些浏览器不支持我只需做少许的CSS Hack技术就可以啊!所以在这里我还要啰嗦两句,CSS中 ...

  4. C++三大库boost、loki、stlport

    转: STL是一个标准,各商家根据这个标准开发了各自的STL版本.而在这形形色色的STL版本中,SGI STL无疑是最引人瞩目的一个.这当然是因为这个STL产品系出名门,其设计和编写者名单中,Alex ...

  5. RPC通信(Windows版、Android版)

    1.RPC通信模型 2.调用截图 服务器端 PC客户端: Android客户端: 3.remotetea jrpcgen.jar:生成Java源文件 oncrpc.jar:框架通信调用 portmap ...

  6. 010GCC程序编译

    1.GCC的特点: Gcc(GUN C Compiler)是GUN推出的功能强大.性能优越的多平台编译器,是GUN的代表作制衣.GCC可以在多种硬体平台上编译出可执行程序,其执行效率与一般的编译器相比 ...

  7. grub2

    手工启动 set  root(hd0,msdos7) linux  /boot/vmlinuz-3.9.8-300.fc19.i686.PAE  root=/dev/sda7 initrd  /boo ...

  8. 根据JSON对象动态加载表格--大数据量

    EasyUI的DataGrid加载数据的时候,如果列数过多(300列以上),数据渲染及其缓慢. JSON对象格式: 1:rowno 2:title 3:colspan 4:rowspan 5:back ...

  9. svn服务器搭建与配置

    http://www.cnblogs.com/armyfai/p/3985660.html

  10. ipv4、ipv6的socket同时监听“bind: Address already in use”的解决方法

    创建ipv4和ipv6的socket,同时监听某个端口的ipv4和ipv6报文,运行时bind函数执行失败,提示“bind: Address already in use”.原因:ipv6的socke ...