我正在使用Retrofit开发Android应用,并尝试解决此问题。
直到昨天,后端人员向我发出以下回应:
[
{"id":1,"key1":"value1", "key2":"value2"},
{"id":2, "key1":"value1", "key2":"value2"}
]
现在,这要改变它:
{"response":
[
{"id":1,"key1":"value1","key2":"value2"},
{"id":2,"key1":"value1","key2":"value2"}
],
"page":1,
"count":2
}
所以,基本上,他们向我发送带有“response”内部对象的数组,然后是一些关于分页的元数据。
现在,我的问题是:我如何用Gson解析它?
目前,我的代码是:
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;
}
}
Retrofit界面:
public interface Api {
@GET("/route")
void listObjects(Callback<List<Object>> callBack);
}
这就是我初始化Retrofit的方式:
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("http://example.com")
.setLogLevel(RestAdapter.LogLevel.FULL)
.build();
api = restAdapter.create(Api.class);
我是如何消费的:
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();
}
});
}
所以我想解析那个,但我无法弄清楚如何用Retrofit和Gson来做。
提前致谢!
答案 0 :(得分:2)
使用This网站我生成了这些pojos。
外层
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;
}
}
内在类
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;
}
}
然后Api电话看起来像这样。
public interface Api {
@GET("/route")
void listObjects(Callback<Response> callBack);
}