I need to show this data in my sample kotlin app.
I have created all appropriate models using moshi. Which can be found here.
But the values are showing wrong. Even non null values are showing as null.
Example json, value in 0 index:
"main": {
"temp": 25.78,
"feels_like": 25.95,
"temp_min": 23.35,
"temp_max": 25.78,
"pressure": 1014,
"sea_level": 1014,
"grnd_level": 1012,
"humidity": 59,
"temp_kf": 2.43
},
The model responsible:
@Parcelize
@JsonClass(generateAdapter = true)
data class Main(
@Json(name = "temp")
val temp: Double?,
@Json(name = "feels_like")
val feelsLike: Double?,
@Json(name = "temp_min")
val tempMin: Double?,
@Json(name = "temp_max")
val tempMax: Double?,
@Json(name = "pressure")
val pressure: Double?,
@Json(name = "sea_level")
val seaLevel: Double?,
@Json(name = "grnd_level")
val grndLevel: Double?,
@Json(name = "humidity")
val humidity: Double?,
@Json(name = "temp_kf")
val tempKf: Double?,
) : Parcelable {
fun getTempString(): String {
return temp.toString().substringBefore(".") "°"
}
fun getHumidityString(): String {
return humidity.toString() "°"
}
fun getTempMinString(): String {
return tempMin.toString().substringBefore(".") "°"
}
fun getTempMaxString(): String {
return tempMax.toString().substringBefore(".") "°"
}
}
But when I print the main in 0 index shows:
Main(temp=298.26, feelsLike=null, tempMin=null, tempMax=null, pressure=1014.0, seaLevel=null, grndLevel=null, humidity=62.0, tempKf=null)
As a result the views are not working either.
The entire source code can be found here.
CodePudding user response:
See discussion here, it might be that the following anotation @field:Json(name = "temp_min")
instead of @Json(name = "temp_min")
, and the same for the other members, could fix it. Alternatively, you can try to use a different version of Moshi. Also, see related discussion Moshi @Json annotation not working for com.github.kittinunf.fuel.moshi.moshiDeserializerOf?