error: Cannot find getter for field. private final com.kbb.webviewolacakmi.model.content icerik = null;
I didn't manage to add the subparts of the json to the room. Thanks to everyone who helped. I would be very happy if you could write a clear code example.
Json File :
{
"date": "xxx",
"title": {
"rendered": "Title"
},
"content": {
"rendered": "content",
"protected": false
},
}
Data Class :
@Entity
data class Icerik(
@ColumnInfo(name="title")
@SerializedName("title")
val baslik:title?,
@ColumnInfo(name="content")
@SerializedName("content")
public val icerik:content?,
@ColumnInfo(name="date")
@SerializedName("date")
val tarih:String?,
@ColumnInfo(name="jetpack_featured_media_url")
@SerializedName("jetpack_featured_media_url")
val gorsel:String?,) {
@PrimaryKey(autoGenerate = true)
var uuid:Int=0
fun getIcerik(){
}
}
data class content(
@ColumnInfo(name="rendered")
@SerializedName("rendered")
public val content: String?,
@ColumnInfo(name="protected")
@SerializedName("protected")
val bool: Boolean?,
){
@PrimaryKey(autoGenerate = true)
var uuid:Int=0
}
data class title(
@ColumnInfo(name="rendered")
@SerializedName("rendered")
val ytitle:String?
){
@PrimaryKey(autoGenerate = true)
var uuid:Int=0
}
IcerikDatabase Class
@TypeConverters(value = [RoomTypeConverters::class])
@Database(entities = arrayOf(Icerik::class), version = 1)
abstract class IcerikDatabase:RoomDatabase() {
abstract fun icerikDao(): IcerikDAO
companion object {
@Volatile private var instance:IcerikDatabase? = null
private val lock=Any()
operator fun invoke(context: Context)= instance?: synchronized(lock){
instance?: databaseOlustur(context).also {
instance=it
}
}
private fun databaseOlustur(context: Context) = Room.databaseBuilder(
context.applicationContext, IcerikDatabase::class.java,
"icerikdatabase"
).build()
}
}
IcerikDao
interface IcerikDAO {
@Insert
suspend fun instertAll(vararg icerik:Icerik):List<Long>
@Query("SELECT * FROM icerik")
suspend fun getAllIcerik():List<Icerik>
@Query("SELECT * FROM icerik WHERE uuid=:icerikId ")
suspend fun getIcerik(icerikId:Int):Icerik
@Query("DELETE FROM icerik")
suspend fun deleteAllIcerik()
}
TypeConverter
class RoomTypeConverters {
@TypeConverter
fun fromTitleToJSONString(title: title?): String? {
return Gson().toJson(title)
}
@TypeConverter
fun toTitleFromJSONString(jsonString: String?): title? {
return Gson().fromJson(jsonString, title::class.java)
}
@TypeConverter
fun fromIcerikToJSONString(content: content?): String? {
return Gson().toJson(content)
}
@TypeConverter
fun toIcrerikFromJSONString(jsonString: String?): content? {
return Gson().fromJson(jsonString, content::class.java)
}
}
I believe that your issue is in regard, not to room, but with the JSON handling.
The JSON file that you have shown cannot directly build an Icerik object.
Rather you need to have an intermediate class that can be built with the JSON and then use that intermediate class to then build the IceRik object.
So you want an intermediate class something along the lines of:-
data class JsonIceRik(
val content: content,
val title: title,
val date: String
)
If the JSON is then amended to be:-
val myjson = "{\"date\": \"xxx\",\"title\": {\"rendered\": \"Title\"},\"content\": {\"rendered\": \"content\",\"protected\": false}}"
Then you could use:-
val m5 = Gson().fromJson(myjson,JsonIceRik::class.java)
To build the intermediate JsonIceRik object.
And then you could use:-
val i5 = Icerik(baslik = m5.title, icerik = m5.content, tarih = m5.date,gorsel = "whatever")
To build the Icerik from the intermediate JsonIceRik.
The result in the database would be:-
You might as well have :-
data class content(
@ColumnInfo(name="rendered")
@SerializedName("rendered")
val content: String?,
@ColumnInfo(name="protected")
@SerializedName("protected")
val bool: Boolean?,
)
data class title(
@ColumnInfo(name="rendered")
@SerializedName("rendered")
val ytitle:String?
)