简体   繁体   中英

Android Jetpack Compose + OKHttp

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

由于 onResponse 不在可组合范围内,因此在 .enqueue(... val coroutineScope = rememberCoroutineScope()之外声明val coroutineScope = rememberCoroutineScope()并在coroutineScope.launch { }启动所需的代码,Toast.makeText(...) 也是如此。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM