25

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.

Karma
  • 1
  • 1
  • 3
  • 9
Yahya-Imam Munir
  • 451
  • 1
  • 5
  • 8
  • 2
    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. – user207421 Feb 02 '18 at 22:44

3 Answers3

119

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.

EdwardG
  • 2,029
  • 2
  • 15
  • 15
19

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.

ck1
  • 5,243
  • 1
  • 21
  • 25
  • 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. – Shai Almog Apr 24 '16 at 03:52
  • 2
    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. – Yahya-Imam Munir Apr 28 '16 at 09:34
3

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.

matrica
  • 31
  • 1