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.
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/
来源:https://stackoverflow.com/questions/44763153/unable-to-create-pojo-with-retrofit-and-java-in-android