KTOR:请确保该类被标记为“@Serialized”并且应用了序列化编译器插件

问题描述 投票:0回答:1

我在使用 Ktor 时遇到此错误:

kotlinx.serialization.SerializationException: Serializer 
for class 'YouTubeResponse' is not found.

日志显示:

Please ensure that class is marked as '@Serializable' and 
that the serialization compiler plugin is applied.

不确定为什么会收到此错误消息。当我记录该服务时,我看到正确的 JSON 响应。我还应用了插件和

@Serializable
注释。

@Serializable
data class YouTubeResponse(
    val kind: String? = null,
    val etag: String? = null,
    val nextPageToken: String? = null,
    val regionCode: String? = null,
    val pageInfo: PageInfo? = null,
    val items: List<Item>? = null
) : Response {

    @Serializable
    data class PageInfo(val totalResults: Int? = null, val resultsPerPage: Int)

    @Serializable
    data class Item(
        val kind: String? = null,
        val etag: String? = null,
        val id: Id? = null,
        val snippet: Snippet? = null
    ) {
        @Serializable
        data class Id(val kind: String? = null, val videoId: String? = null)
    }

    @Serializable
    data class Snippet(
        val publishedAt: String? = null,
        val channelId: String? = null,
        val title: String? = null,
        val description: String? = null,
        val thumbnails: Thumbnails? = null,
        val channelTitle: String? = null,
        val liveBroadcastContent: String? = null,
        val publishTime: String? = null
    ) {
        @Serializable
        data class Thumbnails(
            val default: Default? = null,
            val medium: Medium? = null,
            val high: High? = null
        ) {
            @Serializable
            data class Default(
                val url: String? = null,
                val width: Int = 0,
                val height: Int = 0
            )

            @Serializable
            data class Medium(
                val url: String? = null,
                val width: Int = 0,
                val height: Int = 0
            )

            @Serializable
            data class High(
                val url: String? = null,
                val width: Int = 0,
                val height: Int = 0
            )
        }
    }
}
object KtorSingletons {

    val httpClient: HttpClient by lazy {
        createHttpClient()
    }

    private fun createHttpClient(): HttpClient = HttpClient {
        install(Logging) {
            logger = object : Logger {
                override fun log(message: String) {
                    Log.v("Ktor", message)
                }
            }
            level = LogLevel.ALL
        }
        install(ContentNegotiation) {
            json()
        }
        install(ResponseObserver) {
            onResponse { response ->
                Log.d("HTTP status:", "${response.status.value}")
            }
        }
    }
}

这是我拨打 Ktor 电话的地方:

override suspend fun getYouTubeSnippets(nextPageToken: String?): YouTubeResponse = 
withContext(Dispatchers.IO) {
    val responseBody = client.get(ApiRoutes.YOU_TUBE_SEARCH_URL) {
        method = HttpMethod.Get
        contentType(ContentType.Application.Json)
        parameter(QUERY_KEY_PART, QUERY_VALUE_SNIPPET)
        parameter(QUERY_KEY_CHANNEL_ID, QUERY_VALUE_CHANNEL_ID)
        parameter(QUERY_KEY_ORDER, QUERY_VALUE_ORDER)
        parameter(QUERY_KEY_TYPE, QUERY_VALUE_TYPE)
        parameter(QUERY_KEY_VIDEO_DEFINITION, QUERY_VALUE_VIDEO_DEFINITION)
        parameter(QUERY_KEY_API_KEY, BuildConfig.YOUTUBE_API_KEY)
        nextPageToken?.run {
            parameter("pageToken", nextPageToken)
        }
    }.body<YouTubeResponse>()
    responseBody
}

构建.gradle


plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id "org.jetbrains.kotlin.plugin.serialization" version "1.6.10"
}
kotlin ktor serializable ktor-client
1个回答
0
投票

想通了。我还需要将序列化插件添加到我的项目级别 build.gradle

buildscript {
   ...
} 

plugins {
    id "org.jetbrains.kotlin.plugin.serialization" version "1.6.10"
}

© www.soinside.com 2019 - 2024. All rights reserved.