如何在Kotlin中在Volley请求中添加Url中的Body? [英] How to add Body in Url in Volley request in Kotlin?
本文介绍了如何在Kotlin中在Volley请求中添加Url中的Body?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我对排球比赛的要求:-
Here is my Code that for Volley Request:-
val searchRequest = object : JsonArrayRequest(Request.Method.GET,url,
Response.Listener { response ->
val result = response.toString()
},
Response.ErrorListener { error ->
Toast.makeText(activity, "Error!",Toast.LENGTH_LONG)
.show()
Log.d("ERROR",error.toString())
})
{
override fun getBody(): ByteArray {
// TODO add Body, Header section works //////////
return super.getBody()
}
override fun getBodyContentType(): String {
return "application/json"
}
override fun getHeaders() : Map<String,String> {
val params: MutableMap<String, String> = HashMap()
params["Search-String"] = songName
params["Authorization"] = "Bearer ${accessTx.text}"
return params
}
}
AppController.instance!!.addToRequestQueue(searchRequest)
我想在正文部分添加此信息
I want to add this information in the body section
video_id ="BDJIAH", audio_quality ="256"
video_id = "BDJIAH" , audio_quality = "256"
这是在以下细分中添加上述信息的示例.
here is the sample to add above information in the below segment.
{ "video_id":"ABCDE", "audio_quality":"256" }
{ "video_id":"ABCDE", "audio_quality":"256" }
基本上,我在ByteArray部分遇到问题.那对我不起作用.
Basically, I am facing problem in ByteArray section. That doesn't work for me.
推荐答案
我创建此函数是为了向服务器发送呼叫,这是在呼叫中添加正文的方式.
This function i created to send a call to server and this is how you will add body in your call.
fun sendcall() {
//RequestQueue initialized
mRequestQueue = Volley.newRequestQueue(this)
//String Request initialized
mStringRequest = object : StringRequest(Request.Method.POST, url, Response.Listener { response ->
Toast.makeText(applicationContext, "Logged In Successfully", Toast.LENGTH_SHORT).show()
}, Response.ErrorListener { error ->
Log.i("This is the error", "Error :" + error.toString())
Toast.makeText(applicationContext, "Please make sure you enter correct password and username", Toast.LENGTH_SHORT).show()
}) {
override fun getBodyContentType(): String {
return "application/json"
}
@Throws(AuthFailureError::class)
override fun getBody(): ByteArray {
val params2 = HashMap<String, String>()
params2.put("Login","your credentials" )
params2.put("Password", "your credentials")
return JSONObject(params2).toString().toByteArray()
}
}
mRequestQueue!!.add(mStringRequest!!)
}
这篇关于如何在Kotlin中在Volley请求中添加Url中的Body?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文