Raffu Raffu - 2 months ago 50
reST (reStructuredText) Question

Retrofit, Object inside Array is null

I have already use

Retrofit
with
Google API
, and there is no problem, but since I use my own API, I got a trouble.

So, first, this is my json response

{
"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"
}


well, there is 12 items inside the
array
items.

and I am using
jsonschema2pojo.org
to make the class

And here is my
Retrofit
to retrieve the data

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());


in result in log for
getChannel, getDate, getResponse
is good.
but, in
getDataSchedule
is Item@b4fdbe3, Item@a91f9e0.
The size is returning the correct number of array size.
But if I try to get the
FilmName
, the log shows null.

This is the class that I use to parse the response.

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


How could this happen? I dont get what I've done wrong, thanks for helping.

Answer

I haven't used org.jsonschema2pojo but I guess that Gson cannot parse the film_name in JSON to filmName. Try adding this annotation to the filmName property

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