【问题标题】:Moshi: Failed to find the generated JsonAdapter class for classMoshi:找不到生成的 JsonAdapter 类的类
【发布时间】: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...

我错过了什么?

【问题讨论】:

    标签: kotlin moshi


    【解决方案1】:

    显然,这是因为我将以下内容放在测试文件夹而不是主文件夹中。

    @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
    )
    

    当我将它移动到主文件夹时,它可以工作(因为现在可以生成适配器)

    【讨论】:

      猜你喜欢
      • 2021-09-30
      • 2022-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-18
      • 2020-03-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多