代码生成的适配器构造函数中的`moshi`是什么意思?

时间:2019-07-11 08:24:57

标签: android kotlin moshi

我是kotlin-android开发中的新手。

我想解析一个JSON

{
  "name": "This is my name",
}

定义为一个Foo

Foo.kt

@JsonClass(generateAdapter = true)
data class Foo(val name: String)

Foo类文件通过codegen生成如下代码。

FooJsonAdapter.kt(由代码生成器生成)

class FooJsonAdapter(moshi: Moshi) : JsonAdapter<Foo>() {
    private val options: JsonReader.Options

    private val stringAdapter: JsonAdapter<String>

    override fun toString(): String

    override fun fromJson(reader: JsonReader): Foo 

    override fun toJson(writer: JsonWriter, value: Foo?) 
}

FooJsonAdapter类的构造函数指定Moshi对象,这是我的问题。 什么样的Moshi对象应该传递给构造函数?我尝试了以下操作,但这不起作用。

MainActivity.kt

Moshi.Builder().add(
    FooJsonAdapter(
        Moshi.Builder().build()
    )
).build()

错误消息

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.hoge, PID: 31816
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hoge/com.example.hoge.MainActivity}: java.lang.IllegalArgumentException: Expected at least one @ToJson or @FromJson method on com.example.hoge.FooJsonAdapter
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3260)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3396)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2009)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7319)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934)
     Caused by: java.lang.IllegalArgumentException: Expected at least one @ToJson or @FromJson method on com.example.hoge.FooJsonAdapter
        at com.squareup.moshi.AdapterMethodsFactory.get(AdapterMethodsFactory.java:137)
        at com.squareup.moshi.Moshi$Builder.add(Moshi.java:237)
        at com.example.hoge.MainActivity.onCreate(MainActivity.kt:14)
        at android.app.Activity.performCreate(Activity.java:7783)
        at android.app.Activity.performCreate(Activity.java:7772)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3235)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3396) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2009) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7319) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934) 

我该怎么办?谢谢!

1 个答案:

答案 0 :(得分:2)