改造,数组内的对象为空

Retrofit, Object inside Array is null

我已经把RetrofitGoogle 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 制作 class

这是我的 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,则日志显示为 null。

这是我用来解析响应的class。

@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);
}
}

怎么会这样?我不明白我做错了什么,谢谢你的帮助。

我没有使用过 org.jsonschema2pojo,但我猜 Gson 无法将 JSON 中的 film_name 解析为 filmName。尝试将此注释添加到 filmName 属性

class Item {
    @SerializedName("film_name")
    private String filmName;
     ...
}