要在onResponse方法中使用runOnUiThread方法,可以通过将onSuccess方法包装在runOnUiThread代码块中来实现。修改后的代码如下:

client.newCall(request).enqueue(object : Callback {
    override fun onFailure(call: Call, e: IOException) {
        println(12)
    }

    override fun onResponse(call: Call, response: Response) {
        val request = response.body?.string() ?: return
        runOnUiThread {
            onSuccess.invoke(request)
        }
    }
})

这样,当onResponse方法被调用时,onSuccess方法将在UI线程中执行。请确保在使用runOnUiThread之前,你的代码在一个ActivityFragment的上下文中

object Httputi private val URL = http124931964510091 fun get url String msg String = method String = GET onSuccess String - Unit = val client = OkHttpC

原文地址: http://www.cveoy.top/t/topic/ieQ1 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录