Android Kotlin - Volley 在 JsonArrayRequest 中设置 POST 参数
Android Kotlin - Volley set POST Parameters in JsonArrayRequest
这是代码:
val requestQueue: RequestQueue = Volley.newRequestQueue(this@MainActivity)
val jsonArrayRequest = JsonArrayRequest(
Request.Method.POST,
"$domain/do_getmemes.php",
null,
Response.Listener { response ->
},
Response.ErrorListener { // Do something when error occurred
}
)
requestQueue.add(jsonArrayRequest)
我只想添加一些参数!
我看过这个 JAVA 示例:https://gist.github.com/mstfldmr/f6594b2337e3633673e5
但我不知道 what/where/how 添加来自这个令人厌恶的示例的参数。
我试图在 JsonArrayRequest()
之后添加此部分:
{
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("user","YOUR USERNAME");
params.put("pass","YOUR PASSWORD");
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> params = new HashMap<String, String>();
params.put("Content-Type","application/x-www-form-urlencoded");
return params;
}
}
但它没有转换为 Kotlin。
我需要发送一些整数和字符串
添加对象以便您可以覆盖函数
val jsonArrayRequest = object : JsonArrayRequest(
Request.Method.POST,
"$domain/do_getmemes.php",
null,
Response.Listener { response ->
},
Response.ErrorListener { // Do something when error occurred
}
) {
override fun getBody(): ByteArray {
val parameters = HashMap<String, String>()
parameters["key"] = "value"
return JSONObject(parameters.toString()).toString().toByteArray()
}
}
这是代码:
val requestQueue: RequestQueue = Volley.newRequestQueue(this@MainActivity)
val jsonArrayRequest = JsonArrayRequest(
Request.Method.POST,
"$domain/do_getmemes.php",
null,
Response.Listener { response ->
},
Response.ErrorListener { // Do something when error occurred
}
)
requestQueue.add(jsonArrayRequest)
我只想添加一些参数!
我看过这个 JAVA 示例:https://gist.github.com/mstfldmr/f6594b2337e3633673e5
但我不知道 what/where/how 添加来自这个令人厌恶的示例的参数。
我试图在 JsonArrayRequest()
之后添加此部分:
{
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("user","YOUR USERNAME");
params.put("pass","YOUR PASSWORD");
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> params = new HashMap<String, String>();
params.put("Content-Type","application/x-www-form-urlencoded");
return params;
}
}
但它没有转换为 Kotlin。
我需要发送一些整数和字符串
添加对象以便您可以覆盖函数
val jsonArrayRequest = object : JsonArrayRequest(
Request.Method.POST,
"$domain/do_getmemes.php",
null,
Response.Listener { response ->
},
Response.ErrorListener { // Do something when error occurred
}
) {
override fun getBody(): ByteArray {
val parameters = HashMap<String, String>()
parameters["key"] = "value"
return JSONObject(parameters.toString()).toString().toByteArray()
}
}