Android.system.ErrnoException: isConnected failed: ECONNREFUSED (Connection refused)

16,415

Solution 1

ECONNREFUSED means that the connection was attempted and the remote host port is not listening. Hence this can be caused because of:

Is it a valid IP? check using ifconfig or ipconfig. u can try pinging the server.

Details regarding the error can be understood from here: https://android.googlesource.com/platform/libcore/+/jb-mr2-release/luni/src/main/java/libcore/io/ErrnoException.java https://developer.android.com/reference/android/system/ErrnoException.html

Solution 2

If you are trying to access a mock local server (i.e. localhost) it won't work, because android runs on a separate environment (a device, or emulator), i.e. it has a different localhost than your development machine... you have to publish your server somewhere online.

If you just want a JSON in a REST api, you can use My-Json-Server.

Share:
16,415

Related videos on Youtube

Khanh Luong Van
Author by

Khanh Luong Van

I'm a software Engineer and working in Viet Nam

Updated on May 15, 2022

Comments

  • Khanh Luong Van
    Khanh Luong Van 13 days

    Im using Java connect with MySQL and Json will send data to android, When I send data from Java to json by URL address:

    http://192.168.1.221:9999/rentalcar_service/category/getAllManufacturer
    

    everything's fine, but when I parse data in Android I received an error result following as:

    private static final String URL_MANUFACTURERS = "http://192.168.1.221:9999/rentalcar_service/category/getAllManufacturer";
    

    Logcat:

    W/System.err: Caused by: android.system.ErrnoException: isConnected failed:    ECONNREFUSED (Connection refused)
    W/System.err:     at libcore.io.IoBridge.isConnected(IoBridge.java:223)
    W/System.err:   ... 25 more
    I/MemoryCache: cache size=0 length=4
    W/System.err: java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 9999) after 30000ms: isConnected failed: ECONNREFUSED (Connection refused)
    W/System.err:     at libcore.io.IoBridge.isConnected(IoBridge.java:234)
    W/System.err:     at libcore.io.IoBridge.connectErrno(IoBridge.java:171)
    W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:122)
    

    (192.168.1.221) IP address is my PC, if I change from 192.168.1.221 to localhost, I still receive same error.

    How to fix this problem? Thank so much !

    • Gabe Sechan
      Gabe Sechan over 5 years
      Connection refused means the server wouldn't accept your connection. The problem is going to be in your network setup or your server. Most likely its a firewall in your wifi router.
    • Gabe Sechan
      Gabe Sechan over 5 years
      So you mean you have an emulator on the same machine as the server? In that case, remember that the emulator is not localhost, you have to use the ip address of the PC. And double check firewalls anyway, since you aren't localhost it will go over the network.
  • Khanh Luong Van
    Khanh Luong Van over 5 years
    thank so much ! I change localhost by my IP PC and updated URL in db and its worked ! thanks for your links
  • IgorGanapolsky
    IgorGanapolsky over 3 years
    What about when a remote host is rebooted? Wouldn't that cause a ECONNREFUSED