问题描述
我试图在Kotlin协程内部发出API请求。我已经用try-catch块将所有代码包装在协程中。
viewmodelScope.launch {
try {
val temp = repository.searchMatch(query ?: "")
liveData.postValue(temp)
} catch (e: Exception) {
Log.d(TAG,e.message.toString())
}
}
问题是,如果在请求仍在运行时关闭网络,则会捕获到异常,但由于未捕获的异常,我的应用也会崩溃。
这是我未捕获的异常:
android.system.ErrnoException: read Failed: ENETDOWN (Network is down)
at libcore.io.Linux.readBytes(Native Method)
at libcore.io.Linux.read(Linux.java:184)
at libcore.io.BlockGuardOs.read(BlockGuardOs.java:254)
at android.system.Os.read(Os.java:414)
at android.net.util.PacketReader.readPacket(PacketReader.java:137)
at android.net.util.PacketReader.handleInput(PacketReader.java:207)
at android.net.util.PacketReader.access$100(PacketReader.java:70)
at android.net.util.PacketReader$1.onFileDescriptorEvents(PacketReader.java:189)
at android.os.MessageQueue.dispatchEvents(MessageQueue.java:285)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:326)
at android.os.Looper.loop(Looper.java:165)
at android.os.HandlerThread.run(HandlerThread.java:65)
2020-08-13 23:48:49.326 1205-5265/? E/MtkDhcpClient: Read error
android.system.ErrnoException: read Failed: ENETDOWN (Network is down)
at libcore.io.Linux.readBytes(Native Method)
at libcore.io.Linux.read(Linux.java:184)
at libcore.io.BlockGuardOs.read(BlockGuardOs.java:254)
at android.system.Os.read(Os.java:414)
at com.mediatek.net.dhcp.MtkDhcpClient$ReceiveThread.run(MtkDhcpClient.java:434)
我试图设置一个CoroutineExceptionHandler并使用SupervisorJob,但是没有任何效果。 请帮帮我!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)