I have looked at Java implementations of doing a POST using a JsonObjectRequest. I converted it to Kotlin but Android Studio throws up an error as soon as I add a body to the function (the curly brackets around the getHeaders function).
// Request a string response from the provided URL.
val jsonObjectRequest = JsonObjectRequest( // Error here
Request.Method.POST, url, postData,
Response.Listener<JSONObject> {
fun onResponse(response: JSONObject) {
println(response)
}
},
Response.ErrorListener {
fun onErrorResponse(error: VolleyError) {
error.printStackTrace()
}
})
{ // Error caused by this open bracket
@Override
fun getHeaders() {
val credentials = consumerkey+":"+consumersecret
val headers: HashMap<String, String> = HashMap()
val auth: String = Base64.encodeToString(credentials.toByteArray(), Base64.NO_WRAP)
headers.put("Authorization", "Basic $auth")
}
}
The error shown by Android Studio when I hover the mouse over 'JsonObjectRequest' is this:
None of the following functions can be called with the arguments supplied. <init>(Int, String!, JSONObject?, ((response: JSONObject!) → Unit)!, ((error: VolleyError!) → Unit)?) defined in com.android.volley.toolbox.JsonObjectRequest
<init>(Int, String!, JSONObject?, Response.Listener<JSONObject!>!, Response.ErrorListener?) defined in com.android.volley.toolbox.JsonObjectRequest
<init>(String!, JSONObject?, ((response: JSONObject!) → Unit)!, ((error: VolleyError!) → Unit)?) defined in com.android.volley.toolbox.JsonObjectRequest
<init>(String!, JSONObject?, Response.Listener<JSONObject!>!, Response.ErrorListener?) defined in com.android.volley.toolbox.JsonObjectRequest
What do I need to do to fix this or is there another way to add an authorisation to the header?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…