在Moshi中说明可为空的值

时间:2019-11-19 12:20:53

标签: android kotlin moshi

我有一个这样定义的类:

@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数据时,其中某些值可能为null,也可能不为null。为此,我尝试将@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
}

1 个答案:

答案 0 :(得分:2)

对于空值,应该为

@JsonClass(generateAdapter = true)
data class CurrentWeather(val base: String?=null)

都喜欢其他可空参数。

修改 您应该为上面的json设置像这样的数据类

data class Clouds(
val all: Int)

data class Condition(
val base: String,
val clouds: Clouds,
val cod: Int,
val coord: Coord,
val dt: Int,
val id: Int,
val main: Main,
val name: String,
val sys: Sys,
val timezone: Int,
val visibility: Int,
val weather: List<Weather>,
val wind: Wind
)

data class Coord(
val lat: Double,
val lon: Double
 )


data class Main(
val humidity: Int,
val pressure: Int,
val temp: Double,
val temp_max: Double,
val temp_min: Int
)

data class Sys(
val country: String,
val id: Int,
val sunrise: Int,
val sunset: Int,
val type: Int
 )

data class Weather(
val description: String,
val icon: String,
val id: Int,
val main: String
)


data class Wind(
val deg: Int,
val speed: Double
)