如何在Kotlin中使用齐射将标头放在url中?

时间:2018-08-23 08:34:44

标签: kotlin android-volley kotlin-android-extensions kotlin-extension rx-kotlin

我的代码-

val accessTokenRequest: JsonObjectRequest = JsonObjectRequest(Request.Method.GET, url,
            Response.Listener { response ->
            },
            Response.ErrorListener { error ->
                Toast.makeText(activity,error.toString(), Toast.LENGTH_LONG).show()
            }
    )

    AppController.instance!!.addToRequestQueue(accessTokenRequest)

我想放的标题- “搜索”& “授权”

1 个答案:

答案 0 :(得分:1)

尝试使用以下代码添加标题

val accessTokenRequest: JsonObjectRequest = object : JsonObjectRequest(
    Request.Method.GET, "", JSONObject(),
    Response.Listener<JSONObject?> {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }, Response.ErrorListener {
    TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}) {
    @Throws(AuthFailureError::class)
    override fun getHeaders(): Map<String, String> {
        var params: MutableMap<String, String>? = super.getHeaders()
        if (params == null) params = HashMap()
        params["Authorization"] = "Your authorization"
        //..add other headers
        return params
    }
}

注意:要从Java生成Kotlin代码,请尝试使用Ctrl + Shift + Alt + K或简单地复制粘贴到Kotlin文件中。