gpt4 book ai didi

java - 使用 Kotlin 在 Android 中发送用于 Volley 授权的 header

转载 作者:行者123 更新时间:2023-12-01 19:48:58 25 4
gpt4 key购买 nike

我正在尝试通过在提交注​​册详细信息后发送到用户电子邮件的验证码来对用户进行身份验证。

我有以下代码来监听通过电子邮件收到的代码/PIN,但不确定我是否以正确的方式添加 header /不记名 token ,因为我在验证时收到服务器错误。

请帮忙


var codeEntered:String? = null

txt_pin_entry.setOnPinEnteredListener {
codeEntered = "$it"
Toast.makeText(context!!.applicationContext, "inside $codeEntered", Toast.LENGTH_SHORT).show()
}

submitBtn.setOnClickListener {


val url = "https://fathomless-badlands-69782.herokuapp.com/api/auth/verify"
val params = HashMap<String, String?>()
params["Authorization"] = "Bearer $tokenCode"
params["code"] = codeEntered


Toast.makeText(context!!.applicationContext, "outside $codeEntered", Toast.LENGTH_SHORT).show()


val jsonObject = JSONObject(params as Map<*, *>)

val request = JsonObjectRequest(Request.Method.PUT, url, jsonObject,
Response.Listener { response ->
try{
Log.i("Registration", "Response $response")

}catch (e: Exception){
Log.e("Registration", "Response $e")
Toast.makeText(context!!.applicationContext, "Response $response", Toast.LENGTH_SHORT).show()
}
}, Response.ErrorListener {
Log.e("Registration", "Response $it")
Toast.makeText(context!!.applicationContext, "Response $it", Toast.LENGTH_SHORT).show()
})



VolleySingleton.getInstance(context!!.applicationContext).addToRequestQueue(request)
val actionToVerificationResult = CodeVerificationFragmentDirections.actionToVerificationResult()
Navigation.findNavController(it).navigate(actionToVerificationResult)
}

}

API 需要如下请求

{
code: String
}

最佳答案

你尝试过下面这个吗

override fun getHeaders(): MutableMap<String, String> {
val headers = HashMap<String, String>()
headers["code"] = Your code
return headers
}

示例:

class ServiceVolley : ServiceInterface {
val TAG = ServiceVolley::class.java.simpleName
val basePath = "https://your/backend/api/"

override fun post(path: String, params: JSONObject, completionHandler: (response: JSONObject?) -> Unit) {
val jsonObjReq = object : JsonObjectRequest(Method.POST, basePath + path, params,
Response.Listener<JSONObject> { response ->
Log.d(TAG, "/post request OK! Response: $response")
completionHandler(response)
},
Response.ErrorListener { error ->
VolleyLog.e(TAG, "/post request fail! Error: ${error.message}")
completionHandler(null)
}) {
@Throws(AuthFailureError::class)
override fun getHeaders(): Map<String, String> {
val headers = HashMap<String, String>()
headers.put("Content-Type", "application/json")
return headers
}
}

BackendVolley.instance?.addToRequestQueue(jsonObjReq, TAG)
}
}

关于java - 使用 Kotlin 在 Android 中发送用于 Volley 授权的 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59100994/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com