Android Compose 带参数的 Volley Post 请求

问题描述 投票:0回答:1

我需要在 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)
}
android post parameters android-jetpack-compose android-volley
1个回答
0
投票

您可以重写

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)
}
© www.soinside.com 2019 - 2024. All rights reserved.