connect failed: ECONNREFUSED

81,277

To access your PC localhost from Android emulator, use 10.0.2.2 instead of 127.0.0.1. localhost or 127.0.0.1 refers to the emulated device itself, not the host the emulator is running on.

Reference: https://developer.android.com/studio/run/emulator-networking#networkaddresses

For Genymotion use: 10.0.3.2 instead of 10.0.2.2

Share:
81,277
Ján Srniček
Author by

Ján Srniček

Informatics student/ IT System Developer in Ipesoft Slovakia

Updated on July 09, 2022

Comments

  • Ján Srniček
    Ján Srniček almost 2 years

    I am developing one app, and what I think I wanna do is receive data from server database in android. So I started to run some tutorials. I found one that is doing pretty much what I want. But I am getting:

    detailMessage   "failed to connect to localhost/127.0.0.1 (port 8080): connect failed: ECONNREFUSED (Connection refused)" (id=830021648792) 
    

    Whole code of this tutorial is here link

    Error occurs on :

    OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());

    I checked that:

    • firewall is disabled
    • ping is working
    • connection string is exactly the same as when I run the servlet from server admin interface - http://localhost:8080/Requestor/RQSRV -the servlet is running

    My configuration :

    • Server : Glass Fish 4.0
    • Android App - Developed in Eclipse
    • Servlet - Developed in NetBeans
  • AndroidOptimist
    AndroidOptimist over 10 years
    @Laalto can u please tell me where to change my address to 10.0.2.2 in code
  • Gene Bo
    Gene Bo about 9 years
    For Genymotion 10.0.2.2 won't work. You need to use 10.0.3.2, thanks to: stackoverflow.com/a/20914067/2162226
  • sanjeev51
    sanjeev51 almost 9 years
    This idea worked fine with localhost....but in my office i have different host address 203.131.91.72 Here using url.openConnection() always throws an exception "failed to connect to 203.131.91.72 " What host or proxy permission is required to solve this problem??
  • Manoj
    Manoj almost 9 years
    i too keep on getting this error, i tried to download a file from local host but i keep on geting this error java.net.ConnectException: failed to connect to /30.30.30.38 (port 51749): connect failed: ECONNREFUSED (Connection refused)
  • Mansuu....
    Mansuu.... about 7 years
    I am also facing same issue,but I really don't understand why it is localhost/127.0.0.1:port instead of localhost:port