【发布时间】:2019-07-04 23:37:46
【问题描述】:
我目前正在使用 RetroFit 从网络上检索一个 JSON 对象,我得到的 JSON 对象实际上是一个对象列表。我想将该列表存储到 Room 数据库表中。
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)
}
}
}
然后我创建了一个数据类,让我可以访问 RetroFit 调用的响应。我收到了来自网络的回复,但在房间中存储对象列表时遇到问题。
data class currentNewsResponse(
val news: List<News>){
}
这是我在 Room 中创建的表格示例:
@Entity(tableName = "news", indices = [Index(value = ["newsId"], unique = true)])
data class Feed(
@PrimaryKey
val id: Int
@SerializedName("current_news_id")
val newsId: Int)
谁能给我指出任何相关的文档或通过给我一个关于如何存储对象列表的示例来帮助我?
【问题讨论】:
标签: java android kotlin retrofit2 android-room