Skip to content Skip to sidebar Skip to footer

Java.net.connectexception: Failed To Connect To Localhost/127.0.0.1 (port 80): Connect Failed: Econnrefused (connection Refused)

I am creating an Android app and I have implemented a client server using a WAMP server. I have created my php file, the server is running and a simple version of my php file was i

Solution 1:

java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 80): connect failed: ECONNREFUSED (Connection refused)

String create_url = "http://localhost/myapp/create.php";

is wrong, the correct one is

String create_url = "http://192.168.0.3/myapp/create.php";

being 192.168.0.3 the local ip of your pc (server)

Solution 2:

I got same type of problem. My PC contains MySQL installed and Then I have installed WAMP. Then the problem is occured. WAMP containing mysql port number and my PC's MySQL are conflicting.

So you can uninstall your PC's MySQL first and restart and use WAMP's MySQL. Then the problem will be solved.

Solution 3:

To access localhost you should use local IPv4 + server PORT for localhost.

How to get this IPv4: https://www.whatismybrowser.com/detect/what-is-my-local-ip-address

KOTLIN async request:

privateval client = OkHttpClient()

funrun() {
    val request = Request.Builder()
        .url("http://192.168.0.104:5000/api/post/latest")
        .build()

    client.newCall(request).enqueue(object : Callback {
        overridefunonFailure(call: Call, e: IOException) {
            e.printStackTrace()
        }

        overridefunonResponse(call: Call, response: Response) {
            response.use {
                if (!response.isSuccessful) throw IOException("Unexpected code $response")

                for ((name, value) in response.headers) {
                    println("$name: $value")
                }

                println(response.body!!.string())
            }
        }
    })
}

Install OkHttp: https://square.github.io/okhttp/

Post a Comment for "Java.net.connectexception: Failed To Connect To Localhost/127.0.0.1 (port 80): Connect Failed: Econnrefused (connection Refused)"