微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何使用 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 举报,一经查实,本站将立刻删除。