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

时间:2017-08-23 13:36:34

标签: android kotlin android-studio-3.0

我正在Kotlin写一个Android应用程序,我只是在运行Android API 17的一些华为平板电脑上发生此故障。但是,我已经在索尼Xperia和三星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
            }
        }
    }

App 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开发的第一个应用程序,所以有人可以帮助我了解正在发生的事情以及如何解决此问题?是责备API 17(但app中没有API特定功能的警告),kotlins协同程序,HUAWEI设备?

错误堆栈跟踪按照评论中的要求

  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$000(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$1$1.invoke(login.kt:85)
   W/System.err:     at deeplake.co.uk.livedemokotlin.login$attemptLogin$1$1.invoke(login.kt:26)
   W/System.err:     at org.jetbrains.anko.AsyncKt$uiThread$1.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)

1 个答案:

答案 0 :(得分:1)

我的猜测是,您在此调用中仍在进行网络操作时过早重新进入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
        }
    }

}