Unable to create POJO with Retrofit and Java in android

和自甴很熟 提交于 2019-12-06 16:11:52

You are missing the annotations style for Gson. For example:

public class LiveStreamResponse extends BaseResponse{
  @SerializedName("live_stream")
  @Expose
  private LiveStream liveStream;
  @SerializedName("meta")
  @Expose
  private Meta meta;

  public LiveStream getLiveStream() {
    return liveStream;
  }

  public void setLiveStream(LiveStream liveStream) {
    this.liveStream = liveStream;
  }

  public Meta getMeta() {
    return meta;
  }

  public void setMeta(Meta meta) {
    this.meta = meta;
  }
}

That help Retrofit to match with all objects on your POJO, you have to define which object have to be matched with your "status": "success", "data", "values" from the Json File. You can read more about following this tutorial. Consuming APIs with Retrofit

And also I give you this example using xml and Json.

First you need to identify the objects that are in the JSON structure, in this case there are two.1. The json that you are receiving and 2. data . The option is to create a class for every object.

the first class contains the main object (json itself) with his main attributes: status and data.

public class HistoricalDataContainer implements Parcelable {
    private string status;
    private Data data;
    setters - getters
    ...
}

data is an object, so you need to create his own class to handle his attributes, in this case is an array with string arrays

    public class Data implements Parcelable {
            private List<String> values;
            setters - getters
            ...
        }

To get an specific array inside values you are going to do something like:

List<String> myStringArray = historicalDataContainer.getData().values().get(index);

AND

Why are you using Parcelable?

... I hope this answer is what you need!

You can try http://www.jsonschema2pojo.org/ for JSON Mapping. It's a great tool for creating models from existing JSON responses. And for quick implementation of Parcelable to existing class you can use http://www.parcelabler.com/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!