【发布时间】:2016-09-11 11:31:28
【问题描述】:
我已经用Retrofit和Google API了,没有问题,但是因为我用的是自己的API,所以遇到了麻烦。
首先,这是我的 json 响应
{
"channel": "HBO HD",
"date": "2016-09-08",
"items": [
{
"film_name": "Dead Again",
"film_plot": null,
"show_time": "01:35:00"
},
{
"film_name": "Zeus and Roxanne",
"film_plot": null,
"show_time": "03:20:00"
}
],
"response": "Complete"
}
嗯,array 项目中有 12 个项目。
我正在使用jsonschema2pojo.org 来上课
这是我的Retrofit 来检索数据
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(ROOT_URL)
.addConverterFactory(GsonConverterFactory.create())//GsonConverter untuk parsing json
.build();
RestApi service = retrofit.create(RestApi.class);
Call<ScheduleList> call = service.getScheduleList2
call.enqueue(new Callback<ScheduleList>() {
@Override
public void onResponse(Call<ScheduleList> call, Response<ScheduleList> response) {
try {
loading.dismiss();
Log.d(TAG, "get Channel: " + response.body().getChannel());
Log.d(TAG, "get Date: " + response.body().getDate());
Log.d(TAG, "get Response: " + response.body().getResponse());
Log.d(TAG, "get Item: " + response.body().getItems());
List<Item> test = response.body().getItems();
Log.d(TAG, "size Array Item size: " + test.size());
Log.d(TAG, "get Film Name inside array Item: " + response.body().getItems().get(0).getFilmName());
getChannel, getDate, getResponse 的日志结果很好。
但是,在getDataSchedule 中是 Item@b4fdbe3,Item@a91f9e0。
大小返回正确数量的数组大小。
但是如果我尝试获取FilmName,则日志显示为空。
这是我用来解析响应的类。
@Generated("org.jsonschema2pojo")
public class ScheduleList {
private String channel;
private String date;
private List<Item> items = new ArrayList<Item>();
private String response;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
/**
*
* @return
* The channel
*/
public String getChannel() {
return channel;
}
/**
*
* @param channel
* The channel
*/
public void setChannel(String channel) {
this.channel = channel;
}
/**
*
* @return
* The date
*/
public String getDate() {
return date;
}
/**
*
* @param date
* The date
*/
public void setDate(String date) {
this.date = date;
}
/**
*
* @return
* The items
*/
public List<Item> getItems() {
return items;
}
/**
*
* @param items
* The items
*/
public void setItems(List<Item> items) {
this.items = items;
}
/**
*
* @return
* The response
*/
public String getResponse() {
return response;
}
/**
*
* @param response
* The response
*/
public void setResponse(String response) {
this.response = response;
}
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
@Generated("org.jsonschema2pojo")
public class Item {
private String filmName;
private Object filmPlot;
private String showTime;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
/**
*
* @return
* The filmName
*/
public String getFilmName() {
return filmName;
}
/**
*
* @param filmName
* The film_name
*/
public void setFilmName(String filmName) {
this.filmName = filmName;
}
/**
*
* @return
* The filmPlot
*/
public Object getFilmPlot() {
return filmPlot;
}
/**
*
* @param filmPlot
* The film_plot
*/
public void setFilmPlot(Object filmPlot) {
this.filmPlot = filmPlot;
}
/**
*
* @return
* The showTime
*/
public String getShowTime() {
return showTime;
}
/**
*
* @param showTime
* The show_time
*/
public void setShowTime(String showTime) {
this.showTime = showTime;
}
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
怎么会这样?我不明白我做错了什么,感谢您的帮助。
【问题讨论】:
-
“尺寸是真的”是什么意思?
-
你能发布你用来解析响应的类吗?
-
你确定在使用了jsonschema2pojo之后没有对json或者response类进行修改吗?
-
@kingston 大小是真的我的意思是 test.size() 返回正确的数量数量的项目数组的大小。
-
@kingston 嘿,你可以使用这个链接 [link]homecinema.pe.hu/api/v1/… 进行测试。而且我确定我没有对 json 进行更改
标签: android arrays json rest retrofit2