1.I'm getting the correct response back and can view it in logcat, but I need to save that response and store it for later. I'm trying to save it in the var token, but when looking in memory token remains empty even after correctly getting the response. Any suggestions would be appreciated.
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)
}
}
}
Try moving the intent initialization to when you receive the correct response:
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)
}
Or calling a method when you receive the result:
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)
}
The important thing is you were trying to execute the intent initialization syncronously, but you receive the token async so you token variable was not filled when the intent was executed.