how to fetch information using volley library in kotlin

Viewed 6325

I've a php file on a server, and I have to fetch the information but i don't know how to pass a parameter to the query.

For example, if I have this query: SELECT * FROM accounttable WHERE idaccount = 1, i want to pass this 1 as a parameter, how can i do?

 val stringRequest = StringRequest(Request.Method.POST, URL, Response.Listener<String>{ s ->
        try {

                val array = JSONArray(s)

                for (i in 0..array.length() - 1) {
                    val objectAccount = array.getJSONObject(i)
                    val account = Account(
                            objectAccount.getString("accountplace"),
                            objectAccount.getString("useraccount"),
                            objectAccount.getString("accountpass"))

                    listAccounts.add(account)

                }


        }catch (e: JSONException){
            e.printStackTrace()
        }
    }, Response.ErrorListener { error: VolleyError? -> Log.e("error", "error")  })

    val  requesQueue = Volley.newRequestQueue(this)
    requesQueue.add<String>(stringRequest)
1 Answers

You need to override the getParams() method on your Request. In order to do that you can either subclass StringRequest, or create an object expression (similar to an anonymous inner class from Java); as shown below.

val stringRequest = object : StringRequest(Request.Method.POST, URL, Response.Listener { s ->
    // Your success code here
}, Response.ErrorListener { e ->
    // Your error code here
}) {
    override fun getParams(): Map<String, String> = mapOf("idaccount" to "1")
}
Related