【发布时间】:2022-01-14 19:25:18
【问题描述】:
我正在尽可能接近https://proandroiddev.com/getting-started-using-moshi-for-json-parsing-with-kotlin-5a460bf3935a 中的示例,但仍然无法运行。
在我的 Gradle 中,我有
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
}
和
dependencies {
implementation "com.squareup.moshi:moshi:1.8.0"
kapt 'com.squareup.moshi:moshi-kotlin-codegen:1.8.0'
}
我有课
@JsonClass(generateAdapter = true)
data class Movie (
@Json(name = "vote_count") val voteCount: Int = -1,
val id: Int,
val title: String,
@Json(name = "image_path") val imagePath: String,
val overview: String
)
当我如下运行测试时
@Test
fun testMoshi() {
val moviesJson: String = """
{
"vote_count": 2026,
"id": 19404,
"title": "Example Movie",
"image_path": "/example-movie-image.jpg",
"overview": "Overview of example movie"
}
""".trimIndent()
val moshi: Moshi = Moshi.Builder().build()
val adapter: JsonAdapter<Movie> = moshi.adapter(Movie::class.java)
val movie = adapter.fromJson(moviesJson)
}
失败了Failed to find the generated JsonAdapter class for class...
我错过了什么?
【问题讨论】: