我正在从我的 API 取回一个令牌,并且可以在 Logcat 中打印它 我需要保存该响应,但我没有任何运气。我是科特林的新手

I am getting a token back from my API and can print it in Logcat I need to save that response and I'm not having any luck. I'm new to kotlin

1.I 得到了正确的回复并且可以在 logcat 中查看它,但我需要保存该回复并存储它以备后用。我正在尝试将其保存在 var 令牌中,但是当查看内存时,即使在正确获得响应后令牌仍然为空。如有任何建议,我们将不胜感激。

class Login : AppCompatActivity() {
 var token = ""
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_login)

    //POST data
    log_login.setOnClickListener {
        // "http://localhost:1842/token"
        var url = "https://elimination.azurewebsites.net/token"
        val que = Volley.newRequestQueue(this@Login)
        val stringRequest = object : StringRequest(Request.Method.POST, url,
                Response.Listener { response -> Toast.makeText(this@Login, response, Toast.LENGTH_LONG).show()
                Log.e("Test", response)
                    //trying to set token to response
                token = response},
                Response.ErrorListener { error -> Toast.makeText(this@Login, error.toString(), Toast.LENGTH_LONG).show()
                    Log.e("Wrong", error.toString())}) {

            override fun getParams(): Map<String, String> {
                val params = HashMap<String, String>()
                params.put("grant_type", "password")
                params.put("Username", input_email.toString())
                params.put("Password", input_password.toString())
                return params
            }
        }
        que.add(stringRequest)


        val intent = Intent(this, Profile::class.java)
        intent.putExtra("token", token) //passing token to profile intent
        startActivity(intent)
    }
}

}

尝试将 Intent 初始化移至收到正确响应时:

     log_login.setOnClickListener {
            // "http://localhost:1842/token"
            var url = "https://elimination.azurewebsites.net/token"
            val que = Volley.newRequestQueue(this@Login)
            val stringRequest = object : StringRequest(Request.Method.POST, url,
                    Response.Listener { response -> Toast.makeText(this@Login, response, Toast.LENGTH_LONG).show()
                    Log.e("Test", response)
                        //trying to set token to response
                    token = response
                   val intent = Intent(this, Profile::class.java)
                   intent.putExtra("token", token) 
                   startActivity(intent)

                },
                    Response.ErrorListener { error -> Toast.makeText(this@Login, error.toString(), Toast.LENGTH_LONG).show()
                        Log.e("Wrong", error.toString())}) {

                override fun getParams(): Map<String, String> {
                    val params = HashMap<String, String>()
                    params.put("grant_type", "password")
                    params.put("Username", input_email.toString())
                    params.put("Password", input_password.toString())
                    return params
                }
            }
            que.add(stringRequest)



        }

或者在收到结果时调用方法:

     log_login.setOnClickListener {
            // "http://localhost:1842/token"
            var url = "https://elimination.azurewebsites.net/token"
            val que = Volley.newRequestQueue(this@Login)
            val stringRequest = object : StringRequest(Request.Method.POST, url,
                    Response.Listener { response -> Toast.makeText(this@Login, response, Toast.LENGTH_LONG).show()
                    Log.e("Test", response)
                        //trying to set token to response
                    token = response
                    openNextActivity(token);

                },
                    Response.ErrorListener { error -> Toast.makeText(this@Login, error.toString(), Toast.LENGTH_LONG).show()
                        Log.e("Wrong", error.toString())}) {

                override fun getParams(): Map<String, String> {
                    val params = HashMap<String, String>()
                    params.put("grant_type", "password")
                    params.put("Username", input_email.toString())
                    params.put("Password", input_password.toString())
                    return params
                }
            }
            que.add(stringRequest)

        }

fun openActivity(token: String){
               val intent = Intent(this, Profile::class.java)
               intent.putExtra("token", token) 
               startActivity(intent)

}

重要的是您尝试同步执行意图初始化,但您收到令牌异步,因此在执行意图时未填充令牌变量。