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 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
基于反射:
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://github.com/square/moshi#kotlin
更多关于你的问题https://www.zacsweers.dev/a-closer-look-at-moshi-1-9/ https://www.zacsweers.dev/a-closer-look-at-moshi-1-9/
现在,对于 Kotlin 类,您需要使用代码生成,
KotlinJsonAdapterFactory,或提供您自己的自定义 JsonAdapter。
这是一个潜在危险的变化!在使用代码生成的项目中,
在某些情况下,Kotlin 类可能(似乎)只是
如果您忘记用 @JsonClass 注释它们,请先工作™️。这些
现在将在运行时失败。如果你担心这个,我建议
仅在调试版本中使用 Moshi 1.9 一段时间来戏弄
这些在发布生产版本之前就已经发布了。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)