如何使用Kotlin在排球请求中添加自定义标头 [英] How to add custom header in volley request with kotlin

查看:89
本文介绍了如何使用Kotlin在排球请求中添加自定义标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码Volley Code

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??

推荐答案

我能够在Kotlin中使用以下方法进行操作:

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)

重要的是在构造请求之前使用 object 关键字,以便能够覆盖 getHeaders()方法

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

这篇关于如何使用Kotlin在排球请求中添加自定义标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆