gpt4 book ai didi

android - java.lang.IllegalArgumentException : Platform class java. time.LocalDateTime(没有注释)需要显式 JsonAdapter

转载 作者:行者123 更新时间:2023-12-04 13:40:26 30 4
gpt4 key购买 nike

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

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

在我的模型中,我有一个类 UsersData,其中包含 LocalDateTime 的属性birthDay 类型,问题就在这里,我该如何解决
   data class UsersData (
val id: kotlin.String? = null,
val firstName: kotlin.String? = null,
val lastName: kotlin.String? = null,
val email: kotlin.String? = null,
val phone: kotlin.String? = null,
val image: kotlin.String? = null,
val birthDay: java.time.LocalDateTime? = null,

)

以下代码是适配器的类:
class LocalDateTimeAdapter : JsonAdapter<LocalDateTime>(){
override fun toJson(writer: JsonWriter, value: LocalDateTime?) {
value?.let { writer?.value(it.format(formatter)) }

}

override fun fromJson(reader: JsonReader): LocalDateTime? {
return if (reader.peek() != JsonReader.Token.NULL) {
fromNonNullString(reader.nextString())
} else {
reader.nextNull<Any>()
null
} }
private val formatter = DateTimeFormatter.ISO_LOCAL_DATE
private fun fromNonNullString(nextString: String) : LocalDateTime = LocalDateTime.parse(nextString, formatter)

}

以下代码是 Serializer 对象:
    import com.squareup.moshi.Moshi
import com.squareup.moshi.adapters.Rfc3339DateJsonAdapter
import java.util.*

object Serializer {
@JvmStatic
val moshi: Moshi = Moshi.Builder()
.add(com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory())
.add(LocalDateTime::class.java, LocalDateTimeAdapter().nullSafe())
.build()
}

添加自定义适配器后,出现新错误如下:
java.time.format.DateTimeParseException: Text '2019-09-16T09:00:00Z' could not be parsed, unparsed text found at index 10

最佳答案

您要么需要 Moshi 的明确 LocalDateTime 适配器,要么可以更改 birthDay成为 Date实例。

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

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

关于android - java.lang.IllegalArgumentException : Platform class java. time.LocalDateTime(没有注释)需要显式 JsonAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57972766/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com