2

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"/>
Saman Sattari
  • 3,322
  • 6
  • 30
  • 46

2 Answers2

2

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

Saman Sattari
  • 3,322
  • 6
  • 30
  • 46
  • I have the same problem but with OkHttp and I cannot figure out what the problem is. You said you dialed the windows firewall? Im wondering what the difference is when you running it on the phone? I have a Mac and I tired to turn my firewall off but that didn't work – Jonas Jul 04 '18 at 19:07
  • @Jonas because the host was local on my laptop and somehow windows firewall was blocking the data transfer between phone and laptop – Saman Sattari Jul 07 '18 at 06:42
  • Hi, I have tried all of the above solutions and it's the same for me (not solved). I also have the server running on localhost of my Mac and I am trying to connect an android (5.1.1 OS) to it via same Wi-Fi. – Ionut J. Bejan Nov 15 '21 at 13:42
1

set ip address by checking command prompt(ipconfig) to your .class, Httppost url=("http://192.168.0.116/something.php") and connect your mobile with same wifi address like your system ip address and just restart your mobile it surely works.