Monday 2 December 2019

android - Toast Is not showing in Login Flow when giving call to HTTP request



In my app's login page

I have email & password fields and a sign in button.



The button first gets the username and password, then creates a toast,
and then calls a separate kotlin file to make a http request with the login details.



The weird thing is:
If the credentials are incorrect the toast appears,
but when the credentials are correct and the server responds with data the toast never shows.



I would understand however the order of execution should be that the toast is displayed before the http call is ever made.




btn_signin.setOnClickListener(){
val username = et_username.text.toString().trim()
val password = et_password.text.toString().trim()
var toast_text = "Signing you in..."
Toast.makeText(this@LoginActivity, toast_text, Toast.LENGTH_LONG).show()
val resp = Punchcard().Login(username = username, password = password)
}



Have tried changing the length to short with no difference.
Surely this should display regardless of the following code?



EDIT:



This is in file LoginActivity.kt, I have edited the above code a little.



Another separate file (Punchcard.kt) handles the call to an API on a server, which all works fine and is not causing any issues. It should in theory have absolutely nothing to do with the Toast, it's just odd that the processing in a separate file seems to be taking away from the creation of the Toast. Am I crazy for thinking they should be unrelated and Android's display flow should be line by line?



EDIT2:




This is the Punchcard.kt Login function, makes an okhttp3 call to an API and returns the response as a string. Before it's mentioned I know I shouldn't be passing the password string, this is a flaw in the API I am using but will be updated in future. Also as mentioned above this should not have any effect as the toast is called before the login function.



fun Login(username : String, password : String): String {
val url: String = "http://192.168.8.200:3001/api/auth/signin"
val json = JSONObject()
json.put("usernameOrEmail", username)
json.put("password", password)
val body: RequestBody = RequestBody.create(JSON, json.toString())
val request = Request.Builder()

.url(url)
.post(body)
.build()
val policy : StrictMode.ThreadPolicy = StrictMode.ThreadPolicy.Builder().permitAll().build()
StrictMode.setThreadPolicy(policy)
try {
var response: Response = client.newCall(request).execute()
return response.body()?.string() ?: "error"
} catch (e : Exception) {
println(e)

}
return "error"
}

Answer



Achieved desired result by moving to using async call running on background thread.
Still doesn't explain why the toast doesn't show before the call is made, however in my project this is no longer an issue. It seems that Rahul suggested what I had already done to solve this in comments so good karma to him.
If anyone has an actual explanation for why this happens I'd still be interested to know.


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...