我需要在 Android Kotlin Compose 中使用 Volley 发出请求。 该请求采用 POST 方法,有 2 个参数:电子邮件和令牌。 我提出了一个简单的请求,但我不知道如何添加参数。 有人能帮我吗? 这里
fun PostVolleyRequest(
context: Context,
result: MutableState<String>
) {
val queue = Volley.newRequestQueue(context)
val email = "[email protected]"
val url = "http://127.0.0.1:8000/logged/accedi"
val stringRequest = StringRequest(
Method.POST,
url,
{ response -> result.value = response },
{ println("That didn't work!") }
)
queue.add(stringRequest)
}
您可以重写
getParams()
类中的 StringRequest
方法。
fun PostVolleyRequest(
context: Context,
result: MutableState<String>
) {
val queue = Volley.newRequestQueue(context)
val email = "[email protected]"
val token = "your_token" // replace with your token
val url = "http://127.0.0.1:8000/logged/accedi"
val stringRequest = object : StringRequest(
Method.POST,
url,
{ response -> result.value = response },
{ println("That didn't work!") }
) {
override fun getParams(): Map<String, String> {
val params = HashMap<String, String>()
params["email"] = email
params["token"] = token
return params
}
}
queue.add(stringRequest)
}