2

I have a code Volley Code

 val queue = Volley.newRequestQueue(context)
 val stringRequest = StringRequest(Request.Method.GET, linkTrang,
            Response.Listener<String> { response ->
                mTextView.text = "Response is: " + response.substring(0,500));
            },
            Response.ErrorListener {  })
    {

    }
    queue.add(stringRequest)

How do I set a header called Authorization in this??

Nguyen Thuan
  • 301
  • 1
  • 4
  • 8

1 Answers1

31

I was able to do it in Kotlin using:

    val linkTrang = "YOUR URL"

    val queue = Volley.newRequestQueue(this)

    val stringRequest = object: StringRequest(Request.Method.GET, linkTrang,
        Response.Listener<String> { response ->
            Log.d("A", "Response is: " + response.substring(0,500))
        },
        Response.ErrorListener {  }) 
    {
        override fun getHeaders(): MutableMap<String, String> {
            val headers = HashMap<String, String>()
            headers["Authorization"] = "Basic <<YOUR BASE64 USER:PASS>>"
            return headers
        }
    }

    queue.add(stringRequest)

It is important to use the object keyword before the construction of the request in order to be able to override the getHeaders() method.

Ángel Téllez
  • 927
  • 8
  • 13
Daniel Diehl
  • 643
  • 9
  • 14