Java 存储列表<;对象>;在Android房间数据库中
我目前正在使用改型从web检索一个JSON对象,我得到的JSON对象实际上是一个对象列表。我想将该列表存储到房间数据库表中Java 存储列表<;对象>;在Android房间数据库中,java,android,kotlin,retrofit2,android-room,Java,Android,Kotlin,Retrofit2,Android Room,我目前正在使用改型从web检索一个JSON对象,我得到的JSON对象实际上是一个对象列表。我想将该列表存储到房间数据库表中 interface currentNewsService { @GET("api/feed") fun getFeed( @Query("amount") amount: Int, ): Deferred<currentNewsResponse> companion object{ op
interface currentNewsService {
@GET("api/feed")
fun getFeed(
@Query("amount") amount: Int,
): Deferred<currentNewsResponse>
companion object{
operator fun invoke(): currentNewsService {
val okHttpClient = OkHttpClient.Builder().build()
return Retrofit.Builder().client(okHttpClient)
.baseUrl("https://someUrl")
.addCallAdapterFactory(CoroutineCallAdapterFactory())
.addConverterFactory(GsonConverterFactory.create())
.build().create(currentNewsService::class.java)
}
}
}
有人能给我指一下相关的文档吗?或者给我举个例子,说明如何存储对象列表 房间不允许您直接存储对象的
列表
您要寻找的答案是将对象列表转换为房间允许的类型,并在查询时转换回对象列表
您已经使用了Gson
。因此,在类型转换器中使用Gson
对数据进行序列化/反序列化
有很多关于如何做的教程
data class currentNewsResponse(
val news: List<News>){
}
@Entity(tableName = "news", indices = [Index(value = ["newsId"], unique = true)])
data class Feed(
@PrimaryKey
val id: Int
@SerializedName("current_news_id")
val newsId: Int)