I have backend that return me some json.
我将其解析为我的类:
class SomeData(
@SerializedName("user_name") val name: String,
@SerializedName("user_city") val city: String,
var notNullableValue: String)
Use gson converter factory:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(ENDPOINT)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
And in my interface:
interface MyAPI {
@GET("get_data")
Observable<List<SomeData>> getSomeData();
}
然后,我从服务器检索数据(使用rxJava),没有任何错误.但我预计会出现错误,因为我认为我应该这样做(以防止gson转换器错误,因为我的JSON响应中没有出现notNullableValue
):
class SomeData @JvmOverloads constructor(
@SerializedName("user_name") val name: String,
@SerializedName("user_city") val city: String,
var notNullableValue: String = "")
在从后端接收数据并使用构造函数将数据解析到我的SomeData类(没有def值)之后,notNullableValue == null的值.
据我所知,在kotlin中,不可为空的值可以为空吗?