如何使用 Kotlin 向 Volley JsonObjectRequest 的标头添加授权

问题描述

我查看了使用 JsonObjectRequest 执行 POST 的 Java 实现。我将其转换为 Kotlin,但是一旦我向函数添加主体(getHeaders 函数周围的大括号),Android Studio 就会抛出错误

        // 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")
            }
        }

当我将鼠标悬停在“JsonObjectRequest”上时,Android Studio 显示错误是这样的:

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,Response.Listener<JSONObject!>!,Response.ErrorListener?) defined in com.android.volley.toolBox.JsonObjectRequest
<init>(String!,((error: VolleyError!) → Unit)?) defined in com.android.volley.toolBox.JsonObjectRequest
<init>(String!,Response.ErrorListener?) defined in com.android.volley.toolBox.JsonObjectRequest

我需要怎么做才能解决这个问题,或者还有其他方法可以向标头添加授权吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)