如何在 Kotlin 中使用截击将 header 放入 url 中?
How to put header in url using volley in 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)
Header 我要说的是 -
"Search"&
"Authorization"
尝试使用以下代码添加 headers
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 文件中。
我的代码-
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)
Header 我要说的是 - "Search"& "Authorization"
尝试使用以下代码添加 headers
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 文件中。