java.net.ConnectException: fail to connect to localhost/127.0.0.1(port 8080): connect failed:ECONNREFUSED….(Codename One App)
Asked Answered
A

3

28

After building an android application, I scanned the generated QRcode and install the application on galaxy s4 successfully. But when I try to do some search using the app I got the following exception:

 "java.net.ConnectException: fail to connect to localhost/127.0.0.1(port 8080): connect failed: ECONNREFUSED…." 

Please check the attached picture for more clarity.

The app works correctly on the simulator. I OFF the firewall of my system but that doesn't solve the issue. Please how can I solve this problem?

This issue applies to: NetBeans 8.0.2, GlassFish Server 4.1, Windows 7, Device: Samsung Galaxy S4

enter image description here

Best regards.

Affinitive answered 23/4, 2016 at 13:2 Comment(1)
The attached picture does not add more clarity. Don't post pictures of text here. You've already posted the error message in your title.Balanced
L
22

It looks like you're trying to connect to localhost in your URL.

This probably works fine using the simulator, but you'll need to use an IP address or better yet a resolvable host name in your URL when you run the app in production.

Try changing your URL in this way. You can test for connectivity by using your phone's web browser, for example enter a URL (not using localhost!) of your web service in the browser and see what happens.

Lemuelah answered 23/4, 2016 at 13:20 Comment(2)
Just adding that connecting to an IP might not be enough as the device might reside on a different NAT so he needs to deploy to a public visible server.Wivina
ck1 and Shai thank you for the enlightenment and support. Yes i replaced the localhost with the IP address of the system and the exception disappeared. I'm currently researching on public ip and noip in other to make my server public.Affinitive
J
130

Inside the emulator, 127.0.0.1 refers to the emulator itself - not your local machine. You need to use ip 10.0.2.2, which is bridged to your local machine.

Jeffrey answered 2/2, 2018 at 22:5 Comment(4)
Thank you for the solution, it saved much of my time!Clarkson
Thank you for this answer. Solved my problem that I have benn stuck for days.Fernandefernandel
it is also necessary to rewrite the port e.g. 10.0.2.2:9000Desex
Hi @Morozov, I did not need to rewrite the port, for the record.Jeffrey
L
22

It looks like you're trying to connect to localhost in your URL.

This probably works fine using the simulator, but you'll need to use an IP address or better yet a resolvable host name in your URL when you run the app in production.

Try changing your URL in this way. You can test for connectivity by using your phone's web browser, for example enter a URL (not using localhost!) of your web service in the browser and see what happens.

Lemuelah answered 23/4, 2016 at 13:20 Comment(2)
Just adding that connecting to an IP might not be enough as the device might reside on a different NAT so he needs to deploy to a public visible server.Wivina
ck1 and Shai thank you for the enlightenment and support. Yes i replaced the localhost with the IP address of the system and the exception disappeared. I'm currently researching on public ip and noip in other to make my server public.Affinitive
O
4

I launched the local API server, so I had problems with connecting through IP address.

I found one solution, which helped me - you run your API server, start your emulator, and then you go to android ADB in CMD (path should be something like %USERPROFILE%\AppData\Local\Android\Sdk\platform-tools). Then you go to that directory and run this command:

adb reverse tcp:8080 tcp:8080

and you should be able to reach it. Run your Android app, problem solved.

More about reverse proxy.

Obduliaobdurate answered 3/8, 2020 at 7:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.