java.lang.IllegalArgumentException:平台类 java.time.LocalDateTime (没有注释)需要显式 JsonAdapter

Lin*_*ina 5 android kotlin

在以下网址中参考我的问题:\n将 minsdk 更改为 26 并使用带有 android 9 的模拟器\n出现以下错误

\n\n
E/AndroidRuntime: FATAL EXCEPTION: DefaultDispatcher-worker-4\n    Process: com.app.homecraft, PID: 28831\n    java.lang.IllegalArgumentException: Platform class java.time.LocalDateTime (with no annotations) requires explicit JsonAdapter to be registered\n    for class java.time.LocalDateTime birthDay\n    for class com.app.homecraft.swagger.client.models.UsersData\n        at com.squareup.moshi.Moshi$LookupChain.exceptionWithLookupStack(Moshi.java:348)\n        at com.squareup.moshi.Moshi.adapter(Moshi.java:149)\n        at com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory.create(KotlinJsonAdapter.kt:241)\n        at com.squareup.moshi.Moshi.adapter(Moshi.java:137)\n        at com.squareup.moshi.Moshi.adapter(Moshi.java:97)\n        at com.squareup.moshi.Moshi.adapter(Moshi.java:71)\n        at io.swagger.client.apis.MobileApi.apiMobileUsersGetByFireBaseIDGet(MobileApi.kt:4708)\n        at com.app.homecraft.ui.user.Authentication$signIn$1$1.invokeSuspend(Authentication.kt:87)\n        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)\n        at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:233)\n        at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:594)\n        at kotlinx.coroutines.scheduling.CoroutineScheduler.access$runSafely(CoroutineScheduler.kt:60)\n        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:742)\n     Caused by: java.lang.IllegalArgumentException: Platform class java.time.LocalDateTime (with no annotations) requires explicit JsonAdapter to be registered\n        at com.squareup.moshi.ClassJsonAdapter$1.create(ClassJsonAdapter.java:60)\n        at com.squareup.moshi.Moshi.adapter(Moshi.java:137)\n        at com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory.create(KotlinJsonAdapter.kt:241)\xc2\xa0\n        at com.squareup.moshi.Moshi.adapter(Moshi.java:137)\xc2\xa0\n        at com.squareup.moshi.Moshi.adapter(Moshi.java:97)\xc2\xa0\n        at com.squareup.moshi.Moshi.adapter(Moshi.java:71)\xc2\xa0\n        at io.swagger.client.apis.MobileApi.apiMobileUsersGetByFireBaseIDGet(MobileApi.kt:4708)\xc2\xa0\n        at com.app.homecraft.ui.user.Authentication$signIn$1$1.invokeSuspend(Authentication.kt:87)\xc2\xa0\n        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)\xc2\xa0\n        at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:233)\xc2\xa0\n        at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:594)\xc2\xa0\n        at kotlinx.coroutines.scheduling.CoroutineScheduler.access$runSafely(CoroutineScheduler.kt:60)\xc2\xa0\n        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:742)\xc2\xa0\n
Run Code Online (Sandbox Code Playgroud)\n\n

在我的模型中,我有一个 UsersData 类,其中包含 LocalDateTime 的属性birthDay 类型,问题是我该如何解决它

\n\n
   data class UsersData (\n        val id: kotlin.String? = null,\n        val firstName: kotlin.String? = null,\n        val lastName: kotlin.String? = null,\n        val email: kotlin.String? = null,\n        val phone: kotlin.String? = null,\n        val image: kotlin.String? = null,\n        val birthDay: java.time.LocalDateTime? = null,\n\n    ) \n
Run Code Online (Sandbox Code Playgroud)\n\n

以下代码是适配器的类:

\n\n
class LocalDateTimeAdapter : JsonAdapter<LocalDateTime>(){\n    override fun toJson(writer: JsonWriter, value: LocalDateTime?) {\n        value?.let { writer?.value(it.format(formatter)) }\n\n    }\n\n    override fun fromJson(reader: JsonReader): LocalDateTime? {\n        return if (reader.peek() != JsonReader.Token.NULL) {\n            fromNonNullString(reader.nextString())\n        } else {\n            reader.nextNull<Any>()\n            null\n        }    }\n    private val formatter = DateTimeFormatter.ISO_LOCAL_DATE\n    private fun fromNonNullString(nextString: String) : LocalDateTime = LocalDateTime.parse(nextString, formatter)\n\n  }\n
Run Code Online (Sandbox Code Playgroud)\n\n

下面的代码是Serializer对象:

\n\n
    import com.squareup.moshi.Moshi\n    import com.squareup.moshi.adapters.Rfc3339DateJsonAdapter\n    import java.util.*\n\n object Serializer {\n    @JvmStatic\n    val moshi: Moshi = Moshi.Builder()\n            .add(com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory())\n            .add(LocalDateTime::class.java, LocalDateTimeAdapter().nullSafe())\n            .build()\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

添加自定义适配器后,出现新错误,如下所示:

\n\n
java.time.format.DateTimeParseException: Text '2019-09-16T09:00:00Z' could not be parsed, unparsed text found at index 10\n
Run Code Online (Sandbox Code Playgroud)\n

Bra*_*nsh 1

您要么需要 Moshi 的显式 LocalDateTime 适配器,要么可以更改birthDayDate实例。

编辑:要使用更新的代码解决问题::

ISO_LOCAL_DATE 不支持日期和时间。您需要 ISO_LOCAL_DATE_TIME