仅在某些设备上发生异常崩溃 - 异步调用 kotlin 协程

Unusual crash only on some device - async calls kotlin coroutines

我正在用 Kotlin 编写一个 Android 应用程序,我只在某些 HUAWEI 平板电脑上遇到此崩溃 运行 Android API 17. 但是,我已经测试过了在 Sony Xperia 和 Samsung galaxy s3 mini(或类似的东西)上,运行 Android API 19 都没有问题。崩溃发生在以下代码中 val data = connection.inputStream.bufferedReader().readText() 之前:

doAsync {
        val url = urlString
        val connection = URL(url).openConnection() as HttpURLConnection
        val responseCode = connection.responseCode
        uiThread {
            if (responseCode == 200) {
              // ENTERS IF AND CRASHES WITHOUT EXECUTING THE NEXT LINE
                val data = connection.inputStream.bufferedReader().readText()
                val response = connection.headerFields
                val parser = Parser()
                val stringBuilder = StringBuilder(data)
                val json = parser.parse(stringBuilder) as JsonObject
            }
        }
    }

应用build.gradle

android {
compileSdkVersion 26
buildToolsVersion "26.0.0"
defaultConfig {
    applicationId "appName"
    minSdkVersion 17
    targetSdkVersion 26
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

日志中的消息是 W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x410a3930),之前或之后没有任何其他内容。

这是我为 Android 开发的第一个应用程序,所以有人可以帮助我了解发生了什么以及如何修复此崩溃吗?是不是API17(但是app里面没有提示API特定函数),kotlins协程,华为设备?

错误堆栈跟踪 根据评论中的要求

  W/System.err: android.os.NetworkOnMainThreadException
   W/System.err:     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1120)
   W/System.err:     at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:163)
   W/System.err:     at libcore.io.IoBridge.recvfrom(IoBridge.java:506)
   W/System.err:     at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
   W/System.err:     at java.net.PlainSocketImpl.access[=12=]0(PlainSocketImpl.java:46)
   W/System.err:     at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
   W/System.err:     at java.io.BufferedInputStream.read(BufferedInputStream.java:304)
   W/System.err:     at libcore.net.http.FixedLengthInputStream.read(FixedLengthInputStream.java:45)
   W/System.err:     at java.io.InputStreamReader.read(InputStreamReader.java:244)
   W/System.err:     at java.io.BufferedReader.read(BufferedReader.java:310)
   W/System.err:     at java.io.Reader.read(Reader.java:145)
   W/System.err:     at kotlin.io.TextStreamsKt.copyTo(ReadWrite.kt:117)
   W/System.err:     at kotlin.io.TextStreamsKt.copyTo$default(ReadWrite.kt:114)
   W/System.err:     at kotlin.io.TextStreamsKt.readText(ReadWrite.kt:101)
   W/System.err:     at deeplake.co.uk.livedemokotlin.login$attemptLogin.invoke(login.kt:85)
   W/System.err:     at deeplake.co.uk.livedemokotlin.login$attemptLogin.invoke(login.kt:26)
   W/System.err:     at org.jetbrains.anko.AsyncKt$uiThread.run(Async.kt:70)
   W/System.err:     at android.os.Handler.handleCallback(Handler.java:725)
   W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:92)
   W/System.err:     at android.os.Looper.loop(Looper.java:137)
   W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5119)
   W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
   W/System.err:     at java.lang.reflect.Method.invoke(Method.java:511)
   W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:834)
   W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
   W/System.err:     at dalvik.system.NativeStart.main(Native Method)

我猜你重新进入 UI 线程太早了,因为你还在这个调用中进行网络操作:connection.inputStream.bufferedReader().readText()

你试过这样做吗:

doAsync {
    val url = urlString
    val connection = URL(url).openConnection() as HttpURLConnection
    val responseCode = connection.responseCode

    if (responseCode == 200) {
        val data = connection.inputStream.bufferedReader().readText()
        val response = connection.headerFields
        val parser = Parser()
        val stringBuilder = StringBuilder(data)
        uiThread {
            val json = parser.parse(stringBuilder) as JsonObject
        }
    }

}