i'm developing an Android app, using Retrofit, and trying to resolve this.
The backend guys until yesterday sended to me the following response:
[
{"id":1,"key1":"value1", "key2":"value2"},
{"id":2, "key1":"value1", "key2":"value2"}
]
Now, this wanna change that to this:
{"response":
[
{"id":1,"key1":"value1","key2":"value2"},
{"id":2,"key1":"value1","key2":"value2"}
],
"page":1,
"count":2
}
So, basically, they send me the array with the objects inside "response", and then some meta data about paging.
Now, my question is: how I can parse that with Gson?
Currently, my code is:
The POJO:
public class MyObject{
private int id;
private String key1;
private String key2;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getKey1() {
return key1;
}
public void setKey1(String key1) {
this.key1 = key1;
}
public String getKey2() {
return key2;
}
public void setKey2(String key2) {
this.key2 = key2;
}
}
The Retrofit interface:
public interface Api {
@GET("/route")
void listObjects(Callback<List<Object>> callBack);
}
That is how I initialize Retrofit:
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("http://example.com")
.setLogLevel(RestAdapter.LogLevel.FULL)
.build();
api = restAdapter.create(Api.class);
And how I consume it:
public void getObjects(final Handler handler) {
api.listObjects(new retrofit.Callback<List<Object>>() {
@Override
public void success(List<Object> objects, retrofit.client.Response response) {
handler.onSuccess(objects);
}
@Override
public void failure(RetrofitError error) {
handler.onError();
}
});
}
So I wanna parse that, but I can't figure out how to do it with Retrofit and Gson.
Thanks in advance!