connection refused when trying to connect to my REST server from android emulator

折月煮酒 提交于 2019-12-09 18:05:10

问题


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);

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

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


回答1:


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" />

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

Thanks Adil and rogerstone for your responses.

-Ajmal




回答2:


Replace your url by http://10.0.2.2:8080/server/rest/user/1. This should work.




回答3:


Be sure that you are not running the connection on main thread



来源:https://stackoverflow.com/questions/6547318/connection-refused-when-trying-to-connect-to-my-rest-server-from-android-emulato

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!