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

consider the following Android code and please solve my problem: There is a REST server on my laptop. I can access this server from my browser and get the correct settings ... but now I want to use it from my Android emulator, which also works on my laptop using the 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 the emulator, when I pass the URL as http://www.google.com , I got the correct answer as a result, but when I use my localhost URL (comment one above), I received a communication failure ....

 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 in my browser, it works. can you tell me why localhost url is not working in emulator.?

+6
source share
3 answers

Ankur, I had the same problem, but replacing localhost with 10.0.2.2 worked for me. Also make sure you add the line <uses-permission android: name = "android.permission.INTERNET" />

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

Thanks to Adil and rogerstone for your answers.

-Ajmal

+23
source

Replace the URL http://10.0.2.2:8080/server/rest/user/1 . That should work.

+1
source

Make sure you are not using the connection in the main thread

0
source

Source: https://habr.com/ru/post/891809/


All Articles