克托尔。无法在 kotlin-js 端创建 Httpclient 的实例

Posted

技术标签:

【中文标题】克托尔。无法在 kotlin-js 端创建 Httpclient 的实例【英文标题】:Ktor. Can't create an instance of Httpclient in kotlin-js side 【发布时间】:2020-11-29 06:52:20 【问题描述】:

当我尝试在客户端获取 HttpClient 的实例时,我遵循 js-exception

列表为空。", cause_th0jdv$_0: null, name: “NoSuchElementException”,堆栈:“NoSuchElementException:

不幸的是,遵循解决方法没有帮助

js 
        browser 
            dceTask 
                keep("ktor-ktor-io.\$\$importsForInline\$\$.ktor-ktor-io.io.ktor.utils.io")
            
        
    

SampleJs.kt

package sample

import io.ktor.client.HttpClient
import io.ktor.client.features.json.JsonFeature
import kotlin.browser.document

actual class Sample 
    actual fun checkMe() = 14


actual object Platform 
    actual val name: String = "JS"



@Suppress("unused")
@JsName("helloWorld")
fun helloWorld(salutation: String) 
    val message = "$salutation from Kotlin.JS $hello(), check me value: $Sample().checkMe()"
    document.getElementById("js-response")?.textContent = message


fun main() 
    document.addEventListener("DOMContentLoaded", 
        helloWorld("Hi!")
        val client = HttpClient 
            install(JsonFeature)
        




    )

build.gradle

buildscript 
    repositories 
        jcenter()
    


plugins 
    id 'org.jetbrains.kotlin.multiplatform' version '1.3.72'
    id 'org.jetbrains.kotlin.plugin.serialization' version '1.3.72'
    id 'distribution'


repositories 
    jcenter()
    maven  url "https://dl.bintray.com/kotlin/ktor" 
    mavenCentral()



def ktor_version = '1.3.2'
def logback_version = '1.2.3'
def serialization_version = '0.20.0'


kotlin 
    jvm()
    js 
        browser 
            dceTask 
                keep("ktor-ktor-io.\$\$importsForInline\$\$.ktor-ktor-io.io.ktor.utils.io")
            
        
    
    sourceSets 
        commonMain 
            dependencies 
                implementation kotlin('stdlib-common')
                implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:$serialization_version"
            
        
        commonTest 
            dependencies 
                implementation kotlin('test-common')
                implementation kotlin('test-annotations-common')
            
        
        jvmMain 
            dependencies 
                implementation kotlin('stdlib-jdk8')
                implementation "io.ktor:ktor-server-netty:$ktor_version"
                implementation "io.ktor:ktor-server-servlet:$ktor_version"
                implementation "io.ktor:ktor-html-builder:$ktor_version"
                implementation "ch.qos.logback:logback-classic:$logback_version"
                implementation "io.ktor:ktor-client-core:$ktor_version"
                implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:$serialization_version"
            
        
        jvmTest 
            dependencies 
                implementation kotlin('test')
                implementation kotlin('test-junit')
            
        
        jsMain 
            dependencies 
                implementation npm ("text-encoding", "0.7.0")
                implementation npm ("bufferutil", "4.0.1")
                implementation npm ("utf-8-validate", "5.0.2")
                implementation npm ("abort-controller", "3.0.0")
                implementation npm ("fs", "0.0.1-security")
                implementation kotlin('stdlib-js')
                implementation "io.ktor:ktor-client-json-js:$ktor_version"
                implementation "io.ktor:ktor-client-js:$ktor_version"
                implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-js:1.3.8"
                implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-js:$serialization_version"
            
        
        jsTest 
            dependencies 
                implementation kotlin('test-js')
            
        
    


jvmJar 
    dependsOn(jsBrowserProductionWebpack)
    from(new File(jsBrowserProductionWebpack.entry.name, jsBrowserProductionWebpack.outputPath))


task run(type: JavaExec, dependsOn: [jvmJar]) 
    group = "application"
    main = "sample.SampleJvmKt"
    classpath(configurations.jvmRuntimeClasspath, jvmJar)
    args = []

【问题讨论】:

我也遇到了同样的问题,你找到解决办法了吗? 【参考方案1】:

我遇到了同样的问题并通过添加来解决它

implementation("io.ktor:ktor-client-serialization:1.4.1")

在我的 KotlinJS 项目中。 Ktor 依赖项的完整列表是

implementation("io.ktor:ktor-client-js:1.4.1")  
implementation("io.ktor:ktor-client-serialization:1.4.1")

P.S.:这个解决方案的灵感来自这些票:Ktor Client Serialization JS: Can't obtain default serializer / NoSuchElementExceptionKotlinxSerializer not found in 1.2.0-rc

【讨论】:

以上是关于克托尔。无法在 kotlin-js 端创建 Httpclient 的实例的主要内容,如果未能解决你的问题,请参考以下文章

在 Kotlin-js 中为 HTML 元素设置样式

在 Kotlin 1.3 多平台 gradle 项目中参考来自 kotlin-jvm 的 kotlin-js 资源

html 罗宾斯卡普兰非托尔和托尔

康托尔怎么证明有理数可以枚举

怎么评价康托尔的影响呢?

托尔斯泰经典语录:没有风暴,船帆不过是一块破布