问题
This is the code:
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)
and I just want to add some Parameters!
I've seen this JAVA example: https://gist.github.com/mstfldmr/f6594b2337e3633673e5
but I don't know what/where/how to add the parameters from this abomination of example.
I tried to add this part right after 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;
}
}
but it doesn't get converted to Kotlin.
I need to send some Ints and Strings
回答1:
add object so you can override functions
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()
}
}
来源:https://stackoverflow.com/questions/62461515/android-kotlin-volley-set-post-parameters-in-jsonarrayrequest