zas*_*saz 3 kotlin android-volley
我在服务器上有一个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)
Run Code Online (Sandbox Code Playgroud)
你需要覆盖你的getParams()方法Request.为了做到这一点,你可以子类化StringRequest,或者创建一个对象表达式(类似于Java中的匿名内部类); 如下所示.
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")
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4802 次 |
| 最近记录: |