kotlin协程让安卓开发更简单,代码更简洁

 kotlin{
        experimental {
            coroutines 'enable'
        }
    }
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.1.1'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.1'
  private fun sendIntentQuest() {
        val request = Request.Builder().url("https://www.baidu.com/").build()
        OkHttpClient().newCall(request).enqueue(object : Callback {
            override fun onFailure(call: Call, e: IOException) {
                Log.e(TAG, "onFailure:${e.message}")
            }

            override fun onResponse(call: Call, response: Response) {
//                println(response.body?.string())
                str = response.body!!.string()
                response.close()

                var isMain = Looper.getMainLooper() == Looper.myLooper()
                println("当前线程::$isMain")
                GlobalScope.launch(Dispatchers.Main) {
                    GlobalScope.async(Dispatchers.IO) {
                        println("当前线程:async:${Looper.getMainLooper() == Looper.myLooper()}")

                    }
                    println("当前线程::${Looper.getMainLooper() == Looper.myLooper()}")
                    textView.text = str
                }
            }

        })
    }

你可能感兴趣的:(kotlin)