为什么tor获取此错误父级的作业

发布于 2025-02-05 03:40:27 字数 2397 浏览 2 评论 0原文

我正在为Android客户端使用KTOR,但我有错误。 当我第一次运行该应用程序时,一切都很好,没有问题,但是当我单击设备返回按钮并关闭应用程序并再次打开应用程序时,该应用程序崩溃了,我会收到有关KTOR的错误:

父级完成

这是我的KTOR配置模块:

@InstallIn(SingletonComponent::class)
@Module
object NetworkModule {

    private const val TIME_OUT = 60_000
    @Singleton
    @Provides
    fun provideKtor(): HttpClient = HttpClient(Android) {
        install(HttpCache)
        
        defaultRequest {
            contentType(ContentType.Application.Json)
            accept(ContentType.Application.Json)
        }

        install(ContentNegotiation) {
            json(json = Json {
                prettyPrint = true
                ignoreUnknownKeys = true
                isLenient = true
                encodeDefaults = false
            })
        }

        install(HttpTimeout) {
            connectTimeoutMillis = TIME_OUT.toLong()
            socketTimeoutMillis = TIME_OUT.toLong()
            requestTimeoutMillis = TIME_OUT.toLong()
        }

        install(ResponseObserver) {
            onResponse { response ->
                Log.d("HttpClientLogger - HTTP status", "${response.status.value}")
                Log.d("HttpClientLogger - Response:", response.toString())
            }
        }

        install(Logging) {
            logger = object : Logger {
                override fun log(message: String) {
                    Log.v("Logger Ktor =>", message)
                }

            }
            level = LogLevel.NONE
        }
    }
}
Note: I use ktor version "2.0.2".

    const val ktor_client_core = "io.ktor:ktor-client-core:$ktor_version"
    const val ktor_client_cio = "io.ktor:ktor-client-cio:$ktor_version"
    const val ktor_serialization_json = "io.ktor:ktor-serialization-kotlinx-json:$ktor_version"
    const val ktor_serialization = "io.ktor:ktor-client-serialization:$ktor_version"
    const val ktor_android = "io.ktor:ktor-client-android:$ktor_version"
    const val ktor_negotiation = "io.ktor:ktor-client-content-negotiation:$ktor_version"
    const val ktor_okhttp = "io.ktor:ktor-client-okhttp:$ktor_version"
    const val ktor_logging = "io.ktor:ktor-client-logging:$ktor_version"

如何修复它?

I'm using ktor for the android client but I have an error.
When I run the app for the first time everything is fine and there is no issue, but when I click on the device back button and close the app, and open it again, the app is crashed and I get this error about the ktor:

Parent job is Completed

this is my ktor configure the module:

@InstallIn(SingletonComponent::class)
@Module
object NetworkModule {

    private const val TIME_OUT = 60_000
    @Singleton
    @Provides
    fun provideKtor(): HttpClient = HttpClient(Android) {
        install(HttpCache)
        
        defaultRequest {
            contentType(ContentType.Application.Json)
            accept(ContentType.Application.Json)
        }

        install(ContentNegotiation) {
            json(json = Json {
                prettyPrint = true
                ignoreUnknownKeys = true
                isLenient = true
                encodeDefaults = false
            })
        }

        install(HttpTimeout) {
            connectTimeoutMillis = TIME_OUT.toLong()
            socketTimeoutMillis = TIME_OUT.toLong()
            requestTimeoutMillis = TIME_OUT.toLong()
        }

        install(ResponseObserver) {
            onResponse { response ->
                Log.d("HttpClientLogger - HTTP status", "${response.status.value}")
                Log.d("HttpClientLogger - Response:", response.toString())
            }
        }

        install(Logging) {
            logger = object : Logger {
                override fun log(message: String) {
                    Log.v("Logger Ktor =>", message)
                }

            }
            level = LogLevel.NONE
        }
    }
}
Note: I use ktor version "2.0.2".

    const val ktor_client_core = "io.ktor:ktor-client-core:$ktor_version"
    const val ktor_client_cio = "io.ktor:ktor-client-cio:$ktor_version"
    const val ktor_serialization_json = "io.ktor:ktor-serialization-kotlinx-json:$ktor_version"
    const val ktor_serialization = "io.ktor:ktor-client-serialization:$ktor_version"
    const val ktor_android = "io.ktor:ktor-client-android:$ktor_version"
    const val ktor_negotiation = "io.ktor:ktor-client-content-negotiation:$ktor_version"
    const val ktor_okhttp = "io.ktor:ktor-client-okhttp:$ktor_version"
    const val ktor_logging = "io.ktor:ktor-client-logging:$ktor_version"

How can i fix it?

stackTrace

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌酱 2025-02-12 03:40:27

我发现原因:这与Hilt Di(NetworkModule)有关。我现在必须使用一个对象而不是刀柄模块

I found the reason: This is related to Hilt Di (NetworkModule). I have to use an object instead of hilt module for now

断舍离 2025-02-12 03:40:27

问题在于您不能使用HTTPClient的同一实例。

companion object {
    val client get() = HttpClient(Android) { }
}

The problem is that you cannot use the same instance of the HttpClient.

companion object {
    val client get() = HttpClient(Android) { }
}
梦里寻她 2025-02-12 03:40:27

尝试删除记录插件。我读了一些有关此信息的信息,这是由KTOR记录问题引起的

Try removing logging plugins. I was read some information about that, it caused by ktor logging issue

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文