Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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..?

like image 584
Ankur Avatar asked Jul 01 '11 11:07

Ankur


2 Answers

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

like image 79
MohammadAjmal Avatar answered Oct 31 '22 21:10

MohammadAjmal


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

like image 1
rogerstone Avatar answered Oct 31 '22 21:10

rogerstone