I'm reading some of the another post inside stackoverflow, but I can't get it to work.
I have a data returned by the server, that has the next form:
SERVER RESPONSE
{
"userData": {
"username": "victor.javier",
"email": "victor.javier@stackoverflow.com",
"createdAt": "2016-05-11T09:55:14.720Z",
"updatedAt": "2016-05-11T09:55:14.720Z",
"id": "5733018274ddfad25"
},
"token": "SkpaNuXdzfrhiH06qGK93EH2ujM37hfk02F8o2EodYJumG"
}
My code POJOs are the next:
User.class
public class User {
@SerializedName("userData")
@Expose
private UserData userData;
@SerializedName("token")
@Expose
private String token;
... CONSTRUCTOR AND GETTERS/SETTERS
}
UserData.class
public class UserData {
@SerializedName("username")
@Expose
private String username;
@SerializedName("email")
@Expose
private String email;
@SerializedName("createdAt")
@Expose
private String createdAt;
@SerializedName("updatedAt")
@Expose
private String updatedAt;
@SerializedName("id")
@Expose
private String id;
... CONSTRUCTOR AND GETTERS/SETTERS
}
I had copied from another post the next class to deserialize a GSON. The code is:
RestDeserializer.class
public class RestDeserializer<T> implements JsonDeserializer<T> {
private Class<T> mClass;
private String mKey;
public RestDeserializer(Class<T> targetClass, String key) {
mClass = targetClass;
mKey = key;
}
@Override
public T deserialize(JsonElement je, Type type, JsonDeserializationContext jdc)
throws JsonParseException {
JsonElement content = je.getAsJsonObject().get(mKey);
return new Gson().fromJson(content, mClass);
}
}
The retrofit implementation is:
RetroFitImpl
public class UserApiImpl implements UserApi {
@Inject
public UserApiImpl() { }
@Override
public User loginUser(String identifier, String password) {
Retrofit retrofitBuilder = getRetrofitBuilder();
UserRetrofitApi userRetrofitApi = retrofitBuilder.create(UserRetrofitApi.class);
LoginRequestEntity loginRequestEntity = new LoginRequestEntity();
loginRequestEntity.setIdentifier(identifier);
loginRequestEntity.setPassword(password);
Call<User> call = userRetrofitApi.loginUser(loginRequestEntity);
Response<User> response = null;
try {
response = call.execute();
} catch (IOException e) {
e.printStackTrace();
}
User body = null;
if (response != null) {
body = response.body();
}
return body;
}
private Retrofit getRetrofitBuilder() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
return new Retrofit.Builder()
.baseUrl(BuildConfig.API_URL)
.client(client)
.addConverterFactory(buildGsonConverter())
.build();
}
private static GsonConverterFactory buildGsonConverter() {
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = new GsonBuilder()
.registerTypeAdapter(User.class, new RestDeserializer<>(User.class, "userData"))
.create();
return GsonConverterFactory.create(gson);
}
}
I'm doing something wrong with the buildGsonConverter method because I'm getting a call to onError of retrofit everytime, so I think that it's a problem in the construction of the GSON.
Anyone could tell me how to parsing this to construct a GSON like the response of the server?
Thank you.