开发者

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

开发者 https://www.devze.com 2023-03-16 21:48 出处:网络
consider the followi开发者_JAVA技巧ng 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 wa

consider the followi开发者_JAVA技巧ng 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..?


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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消