androidkotlinandroid-volleyconnectexceptionehostunreach

java.net.ConnectException: failed to connect to /192.168.1.107 (port 80) after 10000ms: isConnected failed: EHOSTUNREACH (No route to host)


I'm trying to connect my REST API from localhost with android application. I'm using xampp as my apache serever on my Windows. The rest api works correctly when I'm trying to get from my computer using this link via Postman:

http://127.0.0.1/RightBoard/Web/right-board/public/api/login

On android device I'm trying to fetch response with Volley. and this is my code:

fun post(url: String, params: MutableMap<String, String>, listener: ResponseListener, token: String = "") {
    val request = object : StringRequest(
            Request.Method.POST,
            url,
            Response.Listener<String> {
                listener.onResponseReceived(JSONObject(it))
            },
            Response.ErrorListener {
                it.printStackTrace()
                if (it.networkResponse?.data != null) {
                    val json = JSONObject(it.networkResponse.data.toString(Charsets.UTF_8))
                    Log.e(Keys.TAG, json.toString())
                }
            }
    ) {
        override fun getParams(): MutableMap<String, String> {
            return params
        }

        override fun getHeaders(): MutableMap<String, String> {
            val headers = HashMap<String, String>()
            headers["Authorization"] = "Bearer $token"
            return headers
        }
    }
    request.retryPolicy = DefaultRetryPolicy(
            10000,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT
    )
    VolleySingleton.instance?.addToRequestQueue(request)
}

url is http://192.168.1.107/RightBoard/Web/right-board/public/api/login and I get the IP="192.168.1.107" from ipconfig. but now I'm getting this error:

java.net.ConnectException: failed to connect to /192.168.1.107 (port 80) after 10000ms: isConnected failed: EHOSTUNREACH (No route to host)

I've checked that both phone and computer are in the same wifi. I run the app in another phone. I even tried restarting computer and xampp... but same error every time. I've also used the permission in my app :

<uses-permission android:name="android.permission.INTERNET"/>

Solution

  • I found the solution. I disabled the Windows Defender Firewall ( In just private section where my network was active)