【发布时间】:2025-12-19 22:30:17
【问题描述】:
我有一个这样定义的类:
@JsonClass(generateAdapter = true)
data class CurrentWeather(
@Json(name = "coord") val coordinates: Coordinates,
@Json(name = "weather") val condition: List<Condition>,
@Json(name = "base") val base: String,
@Json(name = "main") val weatherCondition: Weather,
@Json(name = "wind") val windCondition: Wind,
@Json(name = "clouds") val cloudCondition: Cloud,
@Json(name = "rain") val rainCondition: Rain,
@Json(name = "snow") val snowCondition: Snow,
@Json(name = "dt") val date: Double,
@Json(name = "sys") val sysCondition: Sys,
@Json(name = "id") val cityId: Long,
@Json(name = "name") val cityName: String,
@Json(name = "cod") val status: Int
)
问题是,在获取我的 JSON 数据时,其中一些值可能为空,也可能不为空。为此,我试图在字段名和/或类名之前附加@Nullable,但遗憾的是这种方法不起作用。尝试使用或不使用@Nullable 会给我同样的错误:
2019-11-19 16:52:37.677 1544-1544/com.a5corp.weather W/System.err: com.squareup.moshi.JsonDataException: Required value 'message' missing at $.sys
2019-11-19 16:52:37.677 1544-1544/com.a5corp.weather W/System.err: at com.squareup.moshi.internal.Util.missingProperty(Util.java:605)
2019-11-19 16:52:37.677 1544-1544/com.a5corp.weather W/System.err: at com.a5corp.weather.model.SysJsonAdapter.fromJson(SysJsonAdapter.kt:59)
2019-11-19 16:52:37.677 1544-1544/com.a5corp.weather W/System.err: at com.a5corp.weather.model.SysJsonAdapter.fromJson(SysJsonAdapter.kt:16)
2019-11-19 16:52:37.677 1544-1544/com.a5corp.weather W/System.err: at com.squareup.moshi.internal.NullSafeJsonAdapter.fromJson(NullSafeJsonAdapter.java:40)
2019-11-19 16:52:37.677 1544-1544/com.a5corp.weather W/System.err: at com.a5corp.weather.model.CurrentWeatherJsonAdapter.fromJson(CurrentWeatherJsonAdapter.kt:98)
2019-11-19 16:52:37.677 1544-1544/com.a5corp.weather W/System.err: at com.a5corp.weather.model.CurrentWeatherJsonAdapter.fromJson(CurrentWeatherJsonAdapter.kt:19)
2019-11-19 16:52:37.677 1544-1544/com.a5corp.weather W/System.err: at com.squareup.moshi.internal.NullSafeJsonAdapter.fromJson(NullSafeJsonAdapter.java:40)
2019-11-19 16:52:37.677 1544-1544/com.a5corp.weather W/System.err: at retrofit2.converter.moshi.MoshiResponseBodyConverter.convert(MoshiResponseBodyConverter.java:45)
2019-11-19 16:52:37.677 1544-1544/com.a5corp.weather W/System.err: at retrofit2.converter.moshi.MoshiResponseBodyConverter.convert(MoshiResponseBodyConverter.java:27)
2019-11-19 16:52:37.677 1544-1544/com.a5corp.weather W/System.err: at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:225)
2019-11-19 16:52:37.677 1544-1544/com.a5corp.weather W/System.err: at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:121)
2019-11-19 16:52:37.677 1544-1544/com.a5corp.weather W/System.err: at okhttp3.RealCall$AsyncCall.run(RealCall.kt:138)
2019-11-19 16:52:37.677 1544-1544/com.a5corp.weather W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
2019-11-19 16:52:37.677 1544-1544/com.a5corp.weather W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
2019-11-19 16:52:37.677 1544-1544/com.a5corp.weather W/System.err: at java.lang.Thread.run(Thread.java:919)
我正在通过以下方式使用 Moshi 和 Retrofit:
fun retrofit(url: String): Retrofit = Retrofit.Builder()
.client(owmClient)
.baseUrl(url)
.addConverterFactory(MoshiConverterFactory.create())
.addCallAdapterFactory(CoroutineCallAdapterFactory())
.build()
那么有没有办法在 Moshi 中容纳 Nullable 值?
编辑 1:我现在更改了我的课程,以便在下面的答案中适应 @sasikumar 提供的解决方案,但现在它给了我另一个新错误:
2019-11-19 18:27:00.753 21530-21530/com.a5corp.weather W/System.err: java.lang.NoSuchMethodException: com.a5corp.weather.model.Condition.<init> [int, class java.lang.String, int, class kotlin.jvm.internal.DefaultConstructorMarker]
2019-11-19 18:27:00.754 21530-21530/com.a5corp.weather W/System.err: at java.lang.Class.getConstructor0(Class.java:2332)
2019-11-19 18:27:00.754 21530-21530/com.a5corp.weather W/System.err: at java.lang.Class.getDeclaredConstructor(Class.java:2170)
2019-11-19 18:27:00.754 21530-21530/com.a5corp.weather W/System.err: at com.a5corp.weather.model.ConditionJsonAdapter.fromJson(ConditionJsonAdapter.kt:62)
2019-11-19 18:27:00.754 21530-21530/com.a5corp.weather W/System.err: at com.a5corp.weather.model.ConditionJsonAdapter.fromJson(ConditionJsonAdapter.kt:18)
2019-11-19 18:27:00.754 21530-21530/com.a5corp.weather W/System.err: at com.squareup.moshi.internal.NullSafeJsonAdapter.fromJson(NullSafeJsonAdapter.java:40)
2019-11-19 18:27:00.754 21530-21530/com.a5corp.weather W/System.err: at com.squareup.moshi.CollectionJsonAdapter.fromJson(CollectionJsonAdapter.java:76)
2019-11-19 18:27:00.754 21530-21530/com.a5corp.weather W/System.err: at com.squareup.moshi.CollectionJsonAdapter$2.fromJson(CollectionJsonAdapter.java:53)
2019-11-19 18:27:00.754 21530-21530/com.a5corp.weather W/System.err: at com.squareup.moshi.internal.NullSafeJsonAdapter.fromJson(NullSafeJsonAdapter.java:40)
2019-11-19 18:27:00.754 21530-21530/com.a5corp.weather W/System.err: at com.a5corp.weather.model.CurrentWeatherJsonAdapter.fromJson(CurrentWeatherJsonAdapter.kt:95)
2019-11-19 18:27:00.754 21530-21530/com.a5corp.weather W/System.err: at com.a5corp.weather.model.CurrentWeatherJsonAdapter.fromJson(CurrentWeatherJsonAdapter.kt:22)
2019-11-19 18:27:00.754 21530-21530/com.a5corp.weather W/System.err: at com.squareup.moshi.internal.NullSafeJsonAdapter.fromJson(NullSafeJsonAdapter.java:40)
2019-11-19 18:27:00.754 21530-21530/com.a5corp.weather W/System.err: at retrofit2.converter.moshi.MoshiResponseBodyConverter.convert(MoshiResponseBodyConverter.java:45)
2019-11-19 18:27:00.754 21530-21530/com.a5corp.weather W/System.err: at retrofit2.converter.moshi.MoshiResponseBodyConverter.convert(MoshiResponseBodyConverter.java:27)
2019-11-19 18:27:00.754 21530-21530/com.a5corp.weather W/System.err: at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:225)
2019-11-19 18:27:00.754 21530-21530/com.a5corp.weather W/System.err: at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:121)
2019-11-19 18:27:00.754 21530-21530/com.a5corp.weather W/System.err: at okhttp3.RealCall$AsyncCall.run(RealCall.kt:138)
2019-11-19 18:27:00.754 21530-21530/com.a5corp.weather W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
2019-11-19 18:27:00.754 21530-21530/com.a5corp.weather W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
2019-11-19 18:27:00.754 21530-21530/com.a5corp.weather W/System.err: at java.lang.Thread.run(Thread.java:919)
从第一行开始,我无法理解它的含义,但我确实将我的 Condition 类定义为:
@JsonClass(generateAdapter = true)
data class Condition(
@Json(name = "id") val id: Int? = null,
@Json(name = "description") val description: String? = null
)
编辑 2:基本上是 OWM API:https://openweathermap.org/current 但我的 JSON 数据在这里:
{
"coord": {
"lon": 77.59,
"lat": 12.98
},
"weather": [
{
"id": 802,
"main": "Clouds",
"description": "scattered clouds",
"icon": "03n"
}
],
"base": "stations",
"main": {
"temp": 26.62,
"pressure": 1015,
"humidity": 69,
"temp_min": 23,
"temp_max": 29.44
},
"visibility": 8000,
"wind": {
"speed": 3.1,
"deg": 80
},
"clouds": {
"all": 40
},
"dt": 1574169845,
"sys": {
"type": 1,
"id": 9205,
"country": "IN",
"sunrise": 1574124599,
"sunset": 1574166006
},
"timezone": 19800,
"id": 1277333,
"name": "Bengaluru",
"cod": 200
}
【问题讨论】:
-
你能提供你的json数据吗
-
请检查 EDIT 2