I have backend that return me some json.
I parse it to my class:
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();
}
Then I retrieve data from the server (with rxJava) without any error. But I expected an error because I thought I should do something like this (to prevent GSON converter error, because notNullableValue
is not present in my JSON response):
class SomeData @JvmOverloads constructor(
@SerializedName("user_name") val name: String,
@SerializedName("user_city") val city: String,
var notNullableValue: String = "")
After the data is received from backend and parsed to my SomeData class with constructor without def value, the value of the notNullableValue == null.
As I understand not nullable value can be null in Kotlin?