gpt4 book ai didi

kotlin - 如何在kotlin中使用volley库获取信息

转载 作者:行者123 更新时间:2023-12-02 06:39:44 29 4
gpt4 key购买 nike

我的服务器上有一个 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com