Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 存储列表<;对象>;在Android房间数据库中_Java_Android_Kotlin_Retrofit2_Android Room - Fatal编程技术网

Java 存储列表<;对象>;在Android房间数据库中

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

我目前正在使用改型从web检索一个JSON对象,我得到的JSON对象实际上是一个对象列表。我想将该列表存储到房间数据库表中

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)