尝试从Android模拟器连接到我的REST服务器时连接被拒绝我的、模拟器、连接到、被拒

2023-09-05 08:26:33 作者:可怜楼上月徘徊,情已不再,梦里你怎么还会出现。思君泪尽意悠悠

请考虑以下的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