Some of my server response for success are:
{ "error": null, "data": null }
My model is:
public class BaseResponse<E > {
@Json(name = "error")
public ErrorModel error;
@Json(name = "data")
public E data;
}
If my endpoint is
@POST("user")
Call<BaseResponse<String>> createUser(@Body Credentials credentials);
This works, but the type String is useless as data will always be null. But if i make it:
@POST("user")
Call<BaseResponse> createUser(@Body Credentials credentials);
I got crash at
Call<BaseResponse> call = apiService.createUser(creds);
full log:
java.lang.IllegalArgumentException: Unable to create converter for
class common.model.responses.BaseResponse for method MyEndpoints.createUser
...
Caused by: java.lang.IllegalArgumentException: Expected a Class, ParameterizedType,
or GenericArrayType, but <null> is of type null
at com.squareup.moshi.Types.getRawType(Types.java:167)
at com.squareup.moshi.ClassJsonAdapter$1.createFieldBindings(ClassJsonAdapter.java:83)
at com.squareup.moshi.ClassJsonAdapter$1.create(ClassJsonAdapter.java:75)
at com.squareup.moshi.Moshi.adapter(Moshi.java:100)
at com.squareup.moshi.ClassJsonAdapter$1.createFieldBindings(ClassJsonAdapter.java:91)
at com.squareup.moshi.ClassJsonAdapter$1.create(ClassJsonAdapter.java:75)
at com.squareup.moshi.Moshi.adapter(Moshi.java:100)
at retrofit2.converter.moshi.MoshiConverterFactory.responseBodyConverter(MoshiConverterFactory.java:91)
at retrofit2.Retrofit.nextResponseBodyConverter(Retrofit.java:330)
at retrofit2.Retrofit.responseBodyConverter(Retrofit.java:313)
at retrofit2.ServiceMethod$Builder.createResponseConverter(ServiceMethod.java:736)
...