我在本地跑了一个 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)

最新文章

  1. Java Collection知识总结
  2. MVC认知路【点点滴滴支离破碎】【一】----新建数据库
  3. 第一课 移动端&amp;响应式
  4. 从数学角度看最大期望(EM)算法 I
  5. Selenium2Library系列 keywords 之 _SelectElementKeywords 之 get_selected_list_label(self, locator)
  6. 宜昌IT软件资源汇总
  7. [dp]HDOJ4960 Another OCD Patient
  8. 使用 APPLY
  9. 启动apache服务时报错【the requested operation has failed】
  10. 12.04 ubuntu 更改IP
  11. Linux 系统分区
  12. Thinkphp开启调试模式
  13. eclipse 安装svn和gradle
  14. Modbus库开发笔记之三:Modbus TCP Server开发
  15. Oracle no TOP, how to get top from order
  16. kettle 6.1 通过JS脚本与SwitchCase结合实现目标步骤选择
  17. Docker:Stacks
  18. Python3 中 sys.argv[ ]的用法解释
  19. [转]thinkphp在iis下的rewrite伪静态的配置方法
  20. study mysql

热门文章

  1. php最简单的文件处理。
  2. 学习练习 java 二分查找法
  3. win7突然无法启动(以前可以启动的,电脑是ubuntu+win7双系统)
  4. .Net性能优化时应该关注的数据
  5. b2c项目基础架构分析(二)前端框架 以及补漏的第一篇名词解释
  6. [Ajax] 使用Ajax异步上传图片文件(非Form表单提交)
  7. php 导出csv
  8. Javascript之严格模式详解
  9. Linux命令行选项及参数
  10. mysql批量修改表引擎