0

My code-

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 I wat to put - "Search"& "Authorization"

shivam Kapoor
  • 105
  • 2
  • 3
  • 9
  • Possible duplicate of [Add custom headers in volley request](https://stackoverflow.com/questions/33054019/add-custom-headers-in-volley-request) – user2340612 Aug 23 '18 at 08:40

1 Answers1

8

Try using the following code to add 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
    }
}

Note: To generate kotlin code from java try using Ctrl + Shift + Alt + K or simply copy paste it on a kotlin file.

Abilash
  • 313
  • 5
  • 15
  • Thanks a lot, you made my day. – shivam Kapoor Aug 23 '18 at 15:49
  • 3
    That code results in an error UnsupportedOperationException at java.util.AbstractMap.put. remove the first 2 lines in getHeaders() and just instsantiate a new map right away val params: MutableMap = HashMap() – chitgoks Jul 07 '19 at 05:43