gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:23:31 28 4
gpt4 key购买 nike

我正在用 Kotlin 编写一个 Android 应用程序,我只在一些运行 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
}
}
}

应用构建.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 的问题(但应用程序中没有 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)

最佳答案

我的猜测是您重新进入 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
}
}

}

关于android - 仅在某些设备上发生异常崩溃 - 异步调用 kotlin 协程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45841329/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com