1

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!

2
  • Does this work if you change the following to public? private int id; private String key1; private String key2; Commented Jul 15, 2015 at 21:08
  • I just added more background info to explain it better. Thanks! Commented Jul 15, 2015 at 21:10

1 Answer 1

2

Using This site I generated these pojos.

The outer class

package com.example;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;


public class Response {

@Expose
private List<Response_> response = new ArrayList<Response_>();
@Expose
private Integer page;
@Expose
private Integer count;

/**
* 
* @return
* The response
*/
public List<Response_> getResponse() {
return response;
}

/**
* 
* @param response
* The response
*/
public void setResponse(List<Response_> response) {
this.response = response;
}

/**
* 
* @return
* The page
*/
public Integer getPage() {
return page;
}

/**
* 
* @param page
* The page
*/
public void setPage(Integer page) {
this.page = page;
}

/**
* 
* @return
* The count
*/
public Integer getCount() {
return count;
}

/**
* 
* @param count
* The count
*/
public void setCount(Integer count) {
this.count = count;
}

}

The inner class

package com.example;

import javax.annotation.Generated;
import com.google.gson.annotations.Expose;

public class Response_ {

@Expose
private Integer id;
@Expose
private String key1;
@Expose
private String key2;

/**
* 
* @return
* The id
*/
public Integer getId() {
return id;
}

/**
* 
* @param id
* The id
*/
public void setId(Integer id) {
this.id = id;
}

/**
* 
* @return
* The key1
*/
public String getKey1() {
return key1;
}

/**
* 
* @param key1
* The key1
*/
public void setKey1(String key1) {
this.key1 = key1;
}

/**
* 
* @return
* The key2
*/
public String getKey2() {
return key2;
}

/**
* 
* @param key2
* The key2
*/
public void setKey2(String key2) {
this.key2 = key2;
}

}

Then the Api call looks like this.

public interface Api {

@GET("/route")
void listObjects(Callback<Response> callBack);

}
Sign up to request clarification or add additional context in comments.

2 Comments

Nice! Thanks for the link! It seems like a good solution, but, if I have other objects that are sended in the same way, do I have to do an outer class for every type of object? Suppose they send to me: {"response": [ {"id":1,"key1":"value1","key2":"value2", "key3":"value3"}, {"id":2,"key1":"value1","key2":"value2", "key3":"value3"} ], "page":1, "count":2 } Thanks in advance!
If they are different types of objects then yes you should make a new pojo for each different type of object. If it is the same object with less or more keys in it just put all the keys in the object that you can possibly have. If the key is not there gson will return null for that value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.