【发布时间】:2011-07-01 11:23:27
【问题描述】:
考虑以下 android 代码,请解决我的问题: 我的笔记本电脑上正在运行 REST 服务器..我可以从我的浏览器访问该服务器并获得正确的结果...但是现在我想从我的 android 模拟器中使用它,该模拟器也在我的笔记本电脑上运行,使用以下代码..
// String URL = "http://localhost:8080/server/rest/user/1";
String URL = "http://www.google.com";
HttpClient httpclient = new DefaultHttpClient();
HttpGet request = new HttpGet(URL);
HttpResponse result = httpclient.execute(request);
在模拟器中,当我将 URL 传递为 http://www.google.com 时,我得到了正确的响应,但是当我使用我的本地主机 url(上面注释的那个)时,我得到连接被拒绝....
WARN/System.err(901): org.apache.http.conn.HttpHostConnectException: Connection to http://localhost:8080 refused
WARN/System.err(901): Caused by: java.net.ConnectException: /127.0.0.1:8080 - Connection refused
如果我在浏览器上运行相同的 url,它就可以工作。 你能告诉我为什么 localhost url 在模拟器中不起作用..?
【问题讨论】:
-
Android Emulator中的本地(机器)主机可以通过IP 10.0.2.2访问
-
阿迪尔 我已经试过了,但也没有用..请帮帮我。
-
感谢 Adil 提供这些链接,但所有 3 个链接都建议将 localhost 替换为 10.0.2.2,我已经尝试过,现在出现此错误 java.net.SocketException: Address family not协议支持
-
如果您仍然遇到问题,请随时尝试one