我正在调用天气 API 并取回您在下方看到的 HourlyResponse
。 hourly 字段将一组 JSON 对象映射到我尝试使用 Room 存储的 HourWeatherEntry
实体类列表。现在,在我的 RoomDatabase 实现中,我已经指定了用于 HourWeatherEntry 类的 List<Weather>
字段的自定义类型转换器,尽管如此,我还是收到了粘贴在下面的 SerializationException。我错过了什么,转换器不应该足够吗?为什么我还需要为类本身定义 Weather 类的序列化方法?
HourlyResponse.kt:
data class HourlyResponse(
val hourly: List<HourWeatherEntry>,
val lat: Double,
val lon: Double,
val timezone: String
)
HourWeatherEntry.kt
@Entity(tableName = "hour_weather")
data class HourWeatherEntry(
@SerializedName("weather")
val weatherInfo: List<Weather>,
@SerializedName("temp")
val temperature: Double,
@SerializedName("dt")
val time: Long
) {
@PrimaryKey(autoGenerate = true)
var id:Int = 0
var icon: String? = null
init {
icon = weatherInfo[0].icon
}
}
天气.kt:
data class Weather(
val description: String,
val icon: String,
val id: Int,
val main: String
)
转换器.kt:
class Converters {
val gson = Gson()
@TypeConverter
fun weatherToString(weather: Weather): String = gson.toJson(weather)
@TypeConverter
fun weatherFromString(json: String): Weather = gson.fromJson(json, Weather::class.java)
@TypeConverter
fun fromWeatherList(value: List<Weather>): String = gson.toJson(value)
@TypeConverter
fun toWeatherList(value: String): List<Weather> = Json.decodeFromString(value)
}
预测数据库.kt:
@Database(
entities = [CurrentWeatherEntry::class, HourWeatherEntry::class, WeekDayWeatherEntry::class, WeatherLocation::class],
version = 1
)
@TypeConverters(Converters::class)
abstract class ForecastDatabase : RoomDatabase() {
abstract fun currentWeatherDao(): CurrentWeatherDao
abstract fun hourWeatherDao(): HourWeatherDao
abstract fun weekDayWeatherDao(): WeekDayWeatherDao
abstract fun weatherLocationDao(): WeatherLocationDao
// Used to make sure that the ForecastDatabase class will be a singleton
companion object {
// Volatile == all of the threads will have immediate access to this property
@Volatile
private var instance: ForecastDatabase? = null
private val LOCK = Any() // dummy object for thread monitoring
operator fun invoke(context: Context) = instance ?: synchronized(LOCK) {
// If the instance var hasn't been initialized, call buildDatabase()
// and assign it the returned object from the function call (it)
instance ?: buildDatabase(context).also { instance = it }
}
/**
* Creates an instance of the ForecastDatabase class
* using Room.databaseBuilder().
*/
private fun buildDatabase(context: Context) =
Room.databaseBuilder(
context.applicationContext,
ForecastDatabase::class.java, "forecast.db"
)
//.addMigrations(MIGRATION_2_3) // specify an explicit Migration Technique
.fallbackToDestructiveMigration()
.build()
}
}
异常日志:
java.lang.RuntimeException: Exception while computing database live data.
at androidx.room.RoomTrackingLiveData$1.run(RoomTrackingLiveData.java:92)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:923)
Caused by: kotlinx.serialization.SerializationException: Serializer for class 'Weather' is not found.
Mark the class as @Serializable or provide the serializer explicitly.
at kotlinx.serialization.internal.Platform_commonKt.serializerNotRegistered(Platform.common.kt:91)
at kotlinx.serialization.internal.PlatformKt.platformSpecificSerializerNotRegistered(Platform.kt:29)
at kotlinx.serialization.SerializersKt__SerializersKt.serializer(Serializers.kt:60)
at kotlinx.serialization.SerializersKt.serializer(Unknown Source:1)
at kotlinx.serialization.SerializersKt__SerializersKt.builtinSerializer$SerializersKt__SerializersKt(Serializers.kt:96)
at kotlinx.serialization.SerializersKt__SerializersKt.serializerByKTypeImpl$SerializersKt__SerializersKt(Serializers.kt:84)
at kotlinx.serialization.SerializersKt__SerializersKt.serializer(Serializers.kt:59)
at kotlinx.serialization.SerializersKt.serializer(Unknown Source:1)
at com.nesoinode.flogaweather.model.network.typeconverters.Converters.toWeatherList(Converters.kt:25)
at com.nesoinode.flogaweather.model.db.dao.HourWeatherDao_Impl$3.call(HourWeatherDao_Impl.java:115)
at com.nesoinode.flogaweather.model.db.dao.HourWeatherDao_Impl$3.call(HourWeatherDao_Impl.java:99)
at androidx.room.RoomTrackingLiveData$1.run(RoomTrackingLiveData.java:90)
... 3 more