Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.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
Database 数据类具有内部类时房间数据库表创建错误_Database_Kotlin_Android Room - Fatal编程技术网

Database 数据类具有内部类时房间数据库表创建错误

Database 数据类具有内部类时房间数据库表创建错误,database,kotlin,android-room,Database,Kotlin,Android Room,需求是解析json数据,并使用Kotlin将其插入room数据库。解析已经完成,但在使用data class创建表时,问题出现了,因为data class有一个内部类。无法使用内部类中的字段创建表 无法理解如何使用@TypeConverters @Entity(tableName = "tbl_newsData") @TypeConverters(ReposPersistentConverter::class) data class Article( @PrimaryKey(autoGe

需求是解析json数据,并使用Kotlin将其插入room数据库。解析已经完成,但在使用data class创建表时,问题出现了,因为data class有一个内部类。无法使用内部类中的字段创建表

无法理解如何使用@TypeConverters

@Entity(tableName = "tbl_newsData")
@TypeConverters(ReposPersistentConverter::class)
data class Article(
    @PrimaryKey(autoGenerate = true) var _id: Long?,
    @ColumnInfo(name = "author") var author: String?,
    @ColumnInfo(name = "title") var title: String?,
    @ColumnInfo(name = "description") var description: String?,
    @ColumnInfo(name = "url") var url: String?,
    @ColumnInfo(name = "urlToImage") var urlToImage: String?,
    @ColumnInfo(name = "publishedAt") var publishedAt: String?,
    @ColumnInfo(name = "content") var content: String?,
    var source: Source?
){
    constructor() : this(null,"", "", "", "", "",
        "", "", "", "",null)
}

class ReposPersistentConverter {
    val gson = Gson()
    // RepoOwner
    @TypeConverter
    fun storeRepoOwnerToString(data: Source): String = gson.toJson(data)

    @TypeConverter
    fun storeStringToRepoOwner(value: String): Source = gson.fromJson(value)
}

@Entity(tableName = "tbl_newsData")
data class Article(
    @PrimaryKey(autoGenerate = true) var _id: Long?,
    @ColumnInfo(name = "author") var author: String?,
    @ColumnInfo(name = "title") var title: String?,
    @ColumnInfo(name = "description") var description: String?,
    @ColumnInfo(name = "url") var url: String?,
    @ColumnInfo(name = "urlToImage") var urlToImage: String?,
    @ColumnInfo(name = "publishedAt") var publishedAt: String?,
    @ColumnInfo(name = "content") var content: String?,
    var source: Source?  //inner class
){
    constructor() : this(null,"", "", "", "", "",
        "", "", "", "",null)
}
////源类

data class Source(
    val id: String,
    val name: String
)
//json

{

    "source": {
        "id": null,
        "name": "Geeksofdoom.com"
    },
    "author": "The Movie God",
    "title": "Hulu Comings and Goings: What’s New and What’s Leaving In September 2019",
    "description": "For those of you who do your entertainment streaming on Hulu, whether it be responsibly in moderation or recklessly in full-on binge watching sessions, there's plenty of TV shows and movies arriving and departing each month to keep track of. The titles set to…",
    "url": "https://www.geeksofdoom.com/2019/08/26/hulu-comings-goings-new-leaving-september-2019",
    "urlToImage": "https://www.geeksofdoom.com/GoD/img/2016/02/hulu.jpg",
    "publishedAt": "2019-08-26T18:00:53Z",
    "content": "For those of you who do your entertainment streaming on Hulu, whether it be responsibly in moderation or recklessly in full-on binge watching sessions, there’s plenty of TV shows and movies arriving and departing each month to keep track of.The titles set to … [+8407 chars]"

}
错误:无法确定如何将此字段保存到数据库中。可以考虑为其添加类型转换器。 私有com.app.newsapp.dashboard.model.Source

@Entity(tableName = "tbl_newsData")
data class Article(
    @PrimaryKey(autoGenerate = true) var _id: Long?,
    @ColumnInfo(name = "author") var author: String?,
    @ColumnInfo(name = "title") var title: String?,
    @ColumnInfo(name = "description") var description: String?,
    @ColumnInfo(name = "url") var url: String?,
    @ColumnInfo(name = "urlToImage") var urlToImage: String?,
    @ColumnInfo(name = "publishedAt") var publishedAt: String?,
    @ColumnInfo(name = "content") var content: String?,
    @TypeConverters(SourceTypeConverter::class)
    @ColumnInfo(name = "source")
    var source: Source?
){
    class SourceTypeConverter {
        @TypeConverter
        fun fromDeliveryExchangeList(source: Source?): String? {
            if (source == null) {
                return null
            }
            val gson = Gson()
            val type = object : TypeToken<Source>() {

            }.type
            return gson.toJson(source, type)
        }

        @TypeConverter
        fun toDeliveryExchangeList(source: String?): Source? {
            if (source == null) {
                return null
            }
            val gson = Gson()
            val type = object : TypeToken<Source>() {

            }.type
            return gson.fromJson(source, type)
        }
    }

    constructor() : this(null,"", "", "",
        "", "", "", "",null)
}
@Database(entities = arrayOf(Article::class), version = 1)
@TypeConverters(Article.SourceTypeConverter::class)
abstract class AppDataBase : RoomDatabase() {
}