【问题标题】:Retrofit, Object inside Array is null改造,数组内的对象为空
【发布时间】:2016-09-11 11:31:28
【问题描述】:

我已经用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 来上课

这是我的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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2015-11-28
    • 2016-07-20
    • 2017-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-02
    • 1970-01-01
    • 2017-03-15
    相关资源
    最近更新 更多