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

Ank*_*kur 6 android android-emulator

考虑以下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);
Run Code Online (Sandbox Code Playgroud)

在模拟器中,当我将URL作为http://www.google.com传递时,我得到了正确的结果,但是当我使用我的localhost 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
Run Code Online (Sandbox Code Playgroud)

如果我在我的浏览器上运行相同的URL它可以工作.你能告诉我为什么localhost url不能在模拟器中工作..?

Moh*_*mal 23

Ankur,我有同样的问题,但用10.0.2.2取代localhost为我工作.还要确保添加了<uses-permission android:name ="android.permission.INTERNET"/>行

在AndroidManifest.xml文件的<manifest>标记内.

感谢Adil和rogerstone的回复.

-Ajmal


rog*_*one 1

将您的网址替换为http://10.0.2.2:8080/server/rest/user/1。这应该有效。