使用协程时出现 NetworkOnMainThreadException

NetworkOnMainThreadException while using coroutines

提问人:YSWSL 提问时间:11/8/2020 更新时间:11/8/2020 访问量:2264

问:

我一直在尝试在按下按钮时验证网络连接状态。 特此代码

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    findViewById<Button>(R.id.button).setOnClickListener {
        findViewById<TextView>(R.id.logText).setText("Button Pressed"+counter++)
        GlobalScope.launch (Dispatchers.Main) {
            val result  = withContext(Dispatchers.Default){
                isInternetAvailable()
            }
            println(result.toString())
        }
    }
}

suspend fun isInternetAvailable():Boolean {
    return withContext(Dispatchers.Main) {
        try {
            val ipAddr = InetAddress.getByName("google.com")
            //You can replace it with your name
            println("1$ipAddr")
            return@withContext !ipAddr.equals("")
        } catch (e: Exception) {
            println("within exception$e")
            return@withContext false;
        }

    }
}

但是,当按下按钮时,可以在控制台中看到以下输出。

I/System.out: button clicked
I/System.out: within exceptionandroid.os.NetworkOnMainThreadException
I/System.out: false

有人可以解释NetworkOnMainThread异常的原因吗?

多谢。

kotlin kotlin-coroutines networkonmainthread

评论


答:

4赞 Tenfour04 11/8/2020 #1

withContext(Dispatchers.Main)表示在主线程上的 lambda 中运行代码,这是不允许阻塞代码的。对于网络请求,应使用 .Dispatchers.IO

另外,不要为此使用 GlobalScope,因为它会泄露你的 Activity。用。 默认情况下使用主线程,因此在使用它时,除非您进行阻塞调用,否则不必指定调度程序。lifecycleScopelifecycleScope

按照惯例,每当执行阻塞调用时,都会将挂起函数切换到相应的调度程序,以便您可以安全地从任何调度程序调用它们。这意味着挂起函数不会阻塞调用,并且可以从协程调用,而不必将它们包装在 .withContext

因此,通过上述所有内容,您的代码将变为:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    findViewById<Button>(R.id.button).setOnClickListener {
        findViewById<TextView>(R.id.logText).setText("Button Pressed"+counter++)
        lifecycleScope.launch {
            val result = isInternetAvailable()
            println(result.toString())
        }
    }
}

suspend fun isInternetAvailable(): Boolean {
    return withContext(Dispatchers.IO){
        try {
            val ipAddr = InetAddress.getByName("google.com")
            //You can replace it with your name
            println("1$ipAddr")
            return@withContext !ipAddr.equals("")
        } catch (e: Exception) {
            println("within exception$e")
            return@withContext false;
        }

    }
}