如何在 Kotlin 的 Volley 请求中添加 Body in Url?
How to add Body in Url in Volley request in Kotlin?
这是我的 Volley 请求代码:-
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)
我想在正文部分添加此信息
video_id = "BDJIAH" ,
audio_quality = "256"
这是在下面的片段中添加以上信息的示例。
{
"video_id":"ABCDE",
"audio_quality":"256"
}
基本上,我在 ByteArray 部分遇到了问题。这对我不起作用。
我创建的这个函数是为了向服务器发送调用,这就是您在调用中添加正文的方式。
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!!)
}
Volley post request in kotlin with params
private fun loginUser() {
val username: String = etName.getText().toString().trim()
val password: String = etPass.getText().toString().trim()
val stringRequest: StringRequest = object : StringRequest( Method.POST, LOGINURL,
Response.Listener { response ->
Toast.makeText(this, response, Toast.LENGTH_LONG).show()
try {
val jsonObject = JSONObject(response)
//Parse your api responce here
/*val intent = Intent(this, MainActivity::class.java)
startActivity(intent)*/
} catch (e: JSONException) {
e.printStackTrace()
}
},
Response.ErrorListener { error ->
Toast.makeText(this, error.toString(), Toast.LENGTH_LONG).show()
}) {
override fun getParams(): Map<String, String> {
val params: MutableMap<String, String> = HashMap()
//Change with your post params
params["username"] = username
params["password"] = password
return params
}
}
val requestQueue = Volley.newRequestQueue(this)
requestQueue.add(stringRequest)
}
这是我的 Volley 请求代码:-
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)
我想在正文部分添加此信息
video_id = "BDJIAH" , audio_quality = "256"
这是在下面的片段中添加以上信息的示例。
{ "video_id":"ABCDE", "audio_quality":"256" }
基本上,我在 ByteArray 部分遇到了问题。这对我不起作用。
我创建的这个函数是为了向服务器发送调用,这就是您在调用中添加正文的方式。
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!!)
}
Volley post request in kotlin with params
private fun loginUser() {
val username: String = etName.getText().toString().trim()
val password: String = etPass.getText().toString().trim()
val stringRequest: StringRequest = object : StringRequest( Method.POST, LOGINURL,
Response.Listener { response ->
Toast.makeText(this, response, Toast.LENGTH_LONG).show()
try {
val jsonObject = JSONObject(response)
//Parse your api responce here
/*val intent = Intent(this, MainActivity::class.java)
startActivity(intent)*/
} catch (e: JSONException) {
e.printStackTrace()
}
},
Response.ErrorListener { error ->
Toast.makeText(this, error.toString(), Toast.LENGTH_LONG).show()
}) {
override fun getParams(): Map<String, String> {
val params: MutableMap<String, String> = HashMap()
//Change with your post params
params["username"] = username
params["password"] = password
return params
}
}
val requestQueue = Volley.newRequestQueue(this)
requestQueue.add(stringRequest)
}