6

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

IAmGroot
  • 13,760
  • 18
  • 84
  • 154
Ankur
  • 151
  • 1
  • 3
  • 5
  • 4
    local (machine) host in Android Emulator can be accessed by the IP 10.0.2.2 – Adil Soomro Jul 01 '11 at 11:28
  • Adil I already tried it but that also didn't work..pls help me out. – Ankur Jul 01 '11 at 11:53
  • Have you read these threads [this](http://stackoverflow.com/questions/5495534/java-net-connectexception-localhost-127-0-0-18080-connection-refused/5495789#5495789), [this](http://stackoverflow.com/questions/2301560/java-net-connectexception-127-0-0-18080-an-android-emulator/2301648#2301648) and [this](http://stackoverflow.com/questions/4905315/error-connection-refused/4905367#4905367) – Adil Soomro Jul 01 '11 at 12:03
  • thanks Adil for these links but all 3 links are suggesting to replace localhost by 10.0.2.2, that i have already tried and i got this error now **java.net.SocketException: Address family not supported by protocol** – Ankur Jul 01 '11 at 13:00
  • If you're still facing the issue, feel free to give this [one](https://stackoverflow.com/a/67032554/12298875) a try – Omar Khaium Chowdhury Apr 10 '21 at 09:05

3 Answers3

23

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

MohammadAjmal
  • 231
  • 1
  • 4
  • Adil, android permission for internet was already there in manifest...coz then only i got response for http://www.google.com.. – Ankur Jul 26 '11 at 04:57
  • also have a look at http://stackoverflow.com/questions/11614295/connection-refused-when-trying-to-acces-local-webservice-using-android – Junior Mayhé Mar 10 '14 at 23:47
  • Im at a loss. I can get this working from my android tv but cannot from the android tv emulator. My local server is node.... Any suggestions? – Sealer_05 Feb 07 '16 at 00:29
1

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

rogerstone
  • 7,541
  • 11
  • 53
  • 62
  • It did not work even when i tried replacing localhost with following IP 10.0.2.2 (as suggested [here](http://developer.android.com/resources/faq/commontasks.html#localhostalias)). I also tried accessing it by my absolute IP of my laptop (192.168.1.3) but in vain. However in this case even my browser didn't return me any response. – Ankur Jul 01 '11 at 11:37
0

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

  • If you like, your could improve your answer by elaborating a bit. How can one make sure not to run the connection in the main thread? What will be the consequences if one do? How can it be corrected in the code? – Bex Aug 22 '13 at 16:45