调试中通过android simulator模拟器链接localhost或者127.0.0.1,因为我在电脑上面建立了apache,我的代码大概就是
URL url = new URL(urlString);
URLConnection urlconn = url.openConnection();
但是报错了!!
Exception 1:java.net.ConnectException: localhost/127.0.0.1:8080 -
Connection refused
问题是这样的,android模拟器(simulator)把它自己作为了localhost,也就是说,代码中使用localhost或者 127.0.0.1来访问,都是访问模拟器自己!这是不行的!
你想在模拟器simulator上面访问你的电脑,那么就使用android内置的IP10.0.2.2吧, 10.0.2.2 是模拟器设置的特定ip,是你的电脑的别名alias
,在模拟器上用10.0.2.2访问你的电脑本机
from:http://80x86.javaeye.com/blog/539356
本文解决了一个常见问题:在使用Android模拟器调试时如何正确访问本地主机。通常情况下,模拟器会将localhost解析为自己,导致无法访问到开发者的本地服务器。文章提供了解决方案:使用模拟器内置的IP地址10.0.2.2来代替localhost。
1337

被折叠的 条评论
为什么被折叠?



