我在服务器上有一个 php 文件,我必须获取信息,但我不知道如何将参数传递给查询。

例如,如果我有这个查询:SELECT * FROM accounttable WHERE idaccount = 1,我想把这个 1 作为参数传递,我该怎么做?

 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)

最佳答案

您需要覆盖 getParams() 上的 Request 方法。为此,您可以将 StringRequest 子类化,或者创建一个 object expression(类似于 Java 中的 anonymous inner class);如下所示。

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")
}

关于kotlin - 如何在 kotlin 中使用 volley 库获取信息,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47421447/

10-10 18:25