gpt4 book ai didi

android - Moshi 适配器创建失败 : "requires explicit JsonAdapter to be registered"

转载 作者:行者123 更新时间:2023-12-02 13:29:30 27 4
gpt4 key购买 nike

 var wall= ArrayList<VKWall>()
try {
val response = r.getString("response") as String
val moshi = Moshi.Builder().build()
val type: Type = Types.newParameterizedType(
ArrayList::class.java,
VKWall::class.java
)
val jsonAdapter: JsonAdapter<ArrayList<VKWall>> = moshi.adapter(type)

wall = jsonAdapter.fromJson(response)!!

} catch (e: JSONException){}

return wall

它不能创建适配器。调试器无法执行此字符串并通过此代码进入函数异常
val jsonAdapter: JsonAdapter<ArrayList<VKWall>> = moshi.adapter(type)

我正在尝试像那里一样做所有事情
https://github.com/square/moshi
Platform java.util.ArrayList<com.e.app.fragments.vk_tabs.WallFragment.DataPackage.VKWall> (with no annotations) requires explicit JsonAdapter to be registered


@Parcelize
@JsonClass(



generateAdapter = true)
data class VKWall (
// val UserName:String="",
// val UserSurname:String="",
@Json(name = "text")
val Text:String="" ,
// val attachments: Attachments?,
// val copyright: String="",
// val repost: Repost?
):Parcelable
{


}

最佳答案

问题在于,moshi 没有适用于您的 VKWall 类的适配器。要解决此问题,您可以添加 KotlinJsonAdapterFactory基于反射(reflection):

val moshi = Moshi.Builder()
// ... add your own JsonAdapters and factories ...
.add(KotlinJsonAdapterFactory())
.build()

或者您可以像这样使用生成的适配器:
// Annotate yours class @JsonClass(generateAdapter = true)
@JsonClass(generateAdapter = true)
class VKWall(
....
)

更多关于此的文档 https://github.com/square/moshi#kotlin

更多关于你的问题 https://www.zacsweers.dev/a-closer-look-at-moshi-1-9/

Now, for Kotlin classes, you either need to use code gen, KotlinJsonAdapterFactory, or supply your own custom JsonAdapter. This is a potentially dangerous change! In projects using code gen, there are cases where Kotlin classes could have (appeared to) Just Work™️ before if you forgot to annotate them with @JsonClass. These will fail at runtime now. If you're worried about this, I suggest using Moshi 1.9 only in debug builds for a period of time to tease these out before releasing production builds with it.

关于android - Moshi 适配器创建失败 : "requires explicit JsonAdapter to be registered",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62320292/

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