Unable to create POJO with Retrofit and Java in android

半城伤御伤魂 提交于 2019-12-08 06:33:49

问题


I am getting following json response from one of the vendor.

{
  "status": "success",
  "data": {
  "values": [
      [
        "2015-12-28T09:15:00+0530",
        1386.4,
        1388,
        1381.05,
        1385.1,
        788
      ],
      [
        "2015-12-28T09:15:00+0530",
        1386.4,
        1388,
        1381.05,
        1385.1,
        788
      ]
    ]
  }
}

I would like to convert this to POJO.

I am using retrofit 2.0, rx java.

I have tried following

public class HistoricalDataContainer implements Parcelable{
public String status;
public CandleList data;

protected HistoricalDataContainer(Parcel in) {
    status = in.readString();
    data = in.readParcelable(CandleList.class.getClassLoader());
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(status);
    dest.writeParcelable(data, flags);
}

@Override
public int describeContents() {
    return 0;
}

public static final Creator<HistoricalDataContainer> CREATOR = new Creator<HistoricalDataContainer>() {
    @Override
    public HistoricalDataContainer createFromParcel(Parcel in) {
        return new HistoricalDataContainer(in);
    }

    @Override
    public HistoricalDataContainer[] newArray(int size) {
        return new HistoricalDataContainer[size];
    }
};
}

And

public class CandleList implements Parcelable{
public ArrayList<ArrayList<String>> values = new ArrayList<>();// doesn't work
public ArrayList<String[]> values=new ArrayList<String[]>();// doesn't work
public ArrayList<String>[] values; // doesn't work

protected CandleList(Parcel in) {
}

@Override
public void writeToParcel(Parcel dest, int flags) {
}

@Override
public int describeContents() {
    return 0;
}

public static final Creator<CandleList> CREATOR = new Creator<CandleList>() {
    @Override
    public CandleList createFromParcel(Parcel in) {
        return new CandleList(in);
    }

    @Override
    public CandleList[] newArray(int size) {
        return new CandleList[size];
    }
};
}

But in the above code "value" is always null. What am I missing.


回答1:


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.




回答2:


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!




回答3:


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/



来源:https://stackoverflow.com/questions/44763153/unable-to-create-pojo-with-retrofit-and-java-in-android

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