In trying to use Jetpack Compose with OKHttp, I'm making an HTTP Request, and when it returns if there's a success I want to start a new activity otherwise show the user a toast error message.
However, I'm unsure of how to actually startActivity
. I get the following error(s):
fun performLogin(loginViewModel: LoginViewModel, context: Context) {
try {
val email = loginViewModel.email.value
val password = loginViewModel.password.value
val client = OkHttpClient()
val serverUri = SystemService.getAPIUri()
val loginUri = "$serverUri/login"
val data = """
"email": "$email",
"password": "$password"
""".trimIndent()
val formBody = FormBody.Builder()
.add("email", email.toString())
.add("password", password.toString())
.build()
val body = data.toRequestBody("application/json; charset=utf-8".toMediaTypeOrNull())
val request = Request.Builder()
.url(loginUri)
.post(formBody)
.build()
val call = client.newCall(request)
call.enqueue(object: Callback {
override fun onFailure(call: Call, e: IOException) {
e.printStackTrace()
}
override fun onResponse(call: Call, response: Response) {
response.use {
if (!response.isSuccessful) throw IOException("Unexpected code $response")
for ((name, value) in response.headers) {
println("$name: $value")
}
println(response.body!!.string())
Looper.prepare()
// Gives an error here
loginCallback()
Looper.loop()
}
}
})
} catch (e: Exception) {
println("Login Error: ")
println(e)
val toast = Toast.makeText(context, "Error Logging In", Toast.LENGTH_LONG)
toast.show()
}
}
@Composable
fun loginCallback() {
val context = LocalContext.current
context.startActivity(Intent(context, MainActivity::class.java))
}
If there's an HTTP error the toast will crash saying I have to run it in the Main Thread.
And calling loginCallback() won't work, as it says @Composable invocations can only happen from the context of a @Composable function
As onResponse is outside of composable, declare val coroutineScope = rememberCoroutineScope()
outside of .enqueue(...) and launch desired code within coroutineScope.launch { }
, same goes for Toast.makeText(...).