无法在 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 ...
随机推荐
- android网络判断
//ConnectivityManager管理网络连接相关的操作 ConnectivityManager connectivityManager = (ConnectivityManager) con ...
- centos7 搭建docker内运行rabbitmq,然后再镜像ha方案的完全教程,暂时一个宿主机只能运行一个docker的rabbitmq,但是集群 ha都正常
1.安装centos7.x,配置好网络2.因为docker需要比较高版本的内核,比如使用overlayfs作为默认docker文件系统要3.18,所以先升级内核到3.18以上版本,能直接过4是最佳了检 ...
- C# 发送邮件方法
发送邮件所用的核心知识点 微软封装好的MailMessage类:主要处理发送邮件的内容(如:收发人地址.标题.主体.图片等等) 微软封装好的SmtpClient类:主要处理用smtp方式发送此邮件的配 ...
- Solaris磁盘设备
Solaris的磁盘分区称为分片(slice),在磁盘上,一个环形就是一个分区分片的命名通常是c#t#d#s#c:Controller number,控制器标号,c0就是第一个控制器t:Taget n ...
- 使用C#三维图形控件进行曲线曲面分析
使用AnyCAD.Net三维图图形控件能够计算曲线的切线.法线.曲率.长度等,能够计算曲面的uv切线.法线.面积等. 代码示例一:曲线分析 Platform.LineStyle lineStyle = ...
- java实现身份证校验
原文来自:java教程网 题目:身份证校验 身份证校验 如果让你设计个程序,用什么变量保存身份证号码呢?长整数可以吗?不可以! 因为有人的身份证最后一位是"X" 实 ...
- activiti搭建(三)整合Modeler
转载请注明源地址:http://www.cnblogs.com/lighten/p/5878169.html 接上一章,activiti工作流引擎虽然运行了起来,但是什么都没有.activiti官方在 ...
- 微信开发笔记(一)通过.net如何实现接入微信
微信公众平台,给个人.企业和组织提供业务服务与用户管理能力的全新服务平台.现在基本上每个地方都可以看到微信存在,动不动就是让你扫一下加下微信. 经常遇到这样情况,“到一家餐馆吃饭,拿了个号,前台服务引 ...
- AWS学习之EC2
1.AWS简介 AWS(Amazon Web Services )提供了一整套云计算服务,让您能够构建复杂.可扩展的应用程序.如今,成千上万各种规模的客户都在使用这些云计算服务,它们涉及各个行业,包括 ...
- Cassandra 之 入门
1.到官网下载压缩包. http://cassandra.apache.org/download/ 我下载的是最新的 apache-cassandra-2.1.2-bin.tar.gz 另外:语言支持 ...