尝试从Android模拟器连接到我的REST服务器时连接被拒绝 [英] connection refused when trying to connect to my REST server from android emulator

查看:463
本文介绍了尝试从Android模拟器连接到我的REST服务器时连接被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下的Andr​​oid code和请解决我的问题: 有我的laptop..i可以从我的浏览器访问该服务器,并得到适当的resuts运行REST服务器......但现在我想从我的Andr​​oid模拟器,是在我的笔记本电脑也运行使用下面的code使用它。

consider the following android code and please solve my problem: There is REST server running on my laptop..i can access that server from my browser and get proper resuts...but now i want to use it from my android emulator that is also running on my laptop using following code..

 // 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(在评论上述一个)我有连接被拒绝......

in emulator when i pass the URL as http://www.google.com , i got proper response in result but when i use my localhost url(the commented one above) i got connection refused....

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它的工作原理。  你能告诉我为什么本地主机URL不工作在模拟器..?

if i run the same url on my browser it works. can you tell me why localhost url is not working in emulator..?

推荐答案

ANKUR,我有同样的问题,但与10.0.2.2替换本地主机为我工作。另外,还要确保你添加了这行 <使用-权限的Andr​​oid:名称=android.permission.INTERNET对/>

Ankur, I had the same problem but replacing localhost with 10.0.2.2 worked for me. Also make sure you have added the line <uses-permission android:name="android.permission.INTERNET" />

内的&lt;&舱单GT;标签在AndroidManifest.xml文件。

within the <manifest> tag in the AndroidManifest.xml file.

由于阿迪力和罗杰斯您的答复。

Thanks Adil and rogerstone for your responses.

-Ajmal

这篇关于尝试从Android模拟器连接到我的REST服务器时连接被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆