0
votes

Not able to parse the below json data. Getting success response but the body returned is null Below is the json response I'm getting through web browser.

 {
    "atcocode": "0100BRA10919",
    "smscode": "bstmgtw",
    "request_time": "2018-09-18T09:32:06+01:00",
    "name": "Broomhill Road",
    "stop_name": "Broomhill Road",
    "bearing": "S",
    "indicator": "S-bound",
    "locality": "Brislington, Bristol",
    "location": {
        "type": "Point",
        "coordinates": [-2.54203,
            51.44156
        ]
    },
    "departures": {
        "all": [{
                "mode": "bus",
                "line": "513",
                "line_name": "513",
                "direction": "Brislington",
                "operator": "GB",
                "date": "2018-09-18",
                "expected_departure_date": "2018-09-18",
                "aimed_departure_time": "09:46",
                "expected_departure_time": "09:48",
                "best_departure_estimate": "09:48",
                "source": "NextBuses",
                "dir": "outbound",
                "id": "https://transportapi.com/v3/uk/bus/route/GB/513/outbound/0100BRA10919/2018-09-18/09:46/timetable.json?app_id=1669c89f&app_key=3687b13f180fd5bf688d7df8ea250b0f",
                "operator_name": "Stagecoach West"
            },
            {
                "mode": "bus",
                "line": "513",
                "line_name": "513",
                "direction": "Brislington, Brislington Square",
                "operator": "GB",
                "date": "2018-09-18",
                "expected_departure_date": null,
                "aimed_departure_time": "10:46",
                "expected_departure_time": null,
                "best_departure_estimate": "10:46",
                "source": "NextBuses",
                "dir": "outbound",
                "id": "https://transportapi.com/v3/uk/bus/route/GB/513/outbound/0100BRA10919/2018-09-18/10:46/timetable.json?app_id=1669c89f&app_key=3687b13f180fd5bf688d7df8ea250b0f",
                "operator_name": "Stagecoach West"
            },
            {
                "mode": "bus",
                "line": "513",
                "line_name": "513",
                "direction": "Brislington, Brislington Square",
                "operator": "GB",
                "date": "2018-09-18",
                "expected_departure_date": null,
                "aimed_departure_time": "11:46",
                "expected_departure_time": null,
                "best_departure_estimate": "11:46",
                "source": "NextBuses",
                "dir": "outbound",
                "id": "https://transportapi.com/v3/uk/bus/route/GB/513/outbound/0100BRA10919/2018-09-18/11:46/timetable.json?app_id=1669c89f&app_key=3687b13f180fd5bf688d7df8ea250b0f",
                "operator_name": "Stagecoach West"
            },
            {
                "mode": "bus",
                "line": "513",
                "line_name": "513",
                "direction": "Brislington, Brislington Square",
                "operator": "GB",
                "date": "2018-09-18",
                "expected_departure_date": null,
                "aimed_departure_time": "12:46",
                "expected_departure_time": null,
                "best_departure_estimate": "12:46",
                "source": "NextBuses",
                "dir": "outbound",
                "id": "https://transportapi.com/v3/uk/bus/route/GB/513/outbound/0100BRA10919/2018-09-18/12:46/timetable.json?app_id=1669c89f&app_key=3687b13f180fd5bf688d7df8ea250b0f",
                "operator_name": "Stagecoach West"
            },
            {
                "mode": "bus",
                "line": "513",
                "line_name": "513",
                "direction": "Brislington, Brislington Square",
                "operator": "GB",
                "date": "2018-09-18",
                "expected_departure_date": null,
                "aimed_departure_time": "13:46",
                "expected_departure_time": null,
                "best_departure_estimate": "13:46",
                "source": "NextBuses",
                "dir": "outbound",
                "id": "https://transportapi.com/v3/uk/bus/route/GB/513/outbound/0100BRA10919/2018-09-18/13:46/timetable.json?app_id=1669c89f&app_key=3687b13f180fd5bf688d7df8ea250b0f",
                "operator_name": "Stagecoach West"
            }
        ]
    },
    "source": "NextBuses"
}

Below are the model classes used for parsing the response classes used

    public class All {

    @SerializedName("mode")
    private String mode;
    @SerializedName("line")
    private String line;
    @SerializedName("line_name")

    private String lineName;
    @SerializedName("direction")

    private String direction;
    @SerializedName("operator")

    private String operator;
    @SerializedName("date")

    private String date;
    @SerializedName("expected_departure_date")

    private String expectedDepartureDate;
    @SerializedName("aimed_departure_time")

    private String aimedDepartureTime;
    @SerializedName("expected_departure_time")

    private String expectedDepartureTime;
    @SerializedName("best_departure_estimate")

    private String bestDepartureEstimate;
    @SerializedName("source")

    private String source;
    @SerializedName("dir")

    private String dir;
    @SerializedName("id")

    private String id;
    @SerializedName("operator_name")

    private String operatorName;

    public String getMode() {
        return mode;
    }

    public void setMode(String mode) {
        this.mode = mode;
    }

    public String getLine() {
        return line;
    }

    public void setLine(String line) {
        this.line = line;
    }

    public String getLineName() {
        return lineName;
    }

    public void setLineName(String lineName) {
        this.lineName = lineName;
    }

    public String getDirection() {
        return direction;
    }

    public void setDirection(String direction) {
        this.direction = direction;
    }

    public String getOperator() {
        return operator;
    }

    public void setOperator(String operator) {
        this.operator = operator;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getExpectedDepartureDate() {
        return expectedDepartureDate;
    }

    public void setExpectedDepartureDate(String expectedDepartureDate) {
        this.expectedDepartureDate = expectedDepartureDate;
    }

    public String getAimedDepartureTime() {
        return aimedDepartureTime;
    }

    public void setAimedDepartureTime(String aimedDepartureTime) {
        this.aimedDepartureTime = aimedDepartureTime;
    }

    public String getExpectedDepartureTime() {
        return expectedDepartureTime;
    }

    public void setExpectedDepartureTime(String expectedDepartureTime) {
        this.expectedDepartureTime = expectedDepartureTime;
    }

    public String getBestDepartureEstimate() {
        return bestDepartureEstimate;
    }

    public void setBestDepartureEstimate(String bestDepartureEstimate) {
        this.bestDepartureEstimate = bestDepartureEstimate;
    }

    public String getSource() {
        return source;
    }

    public void setSource(String source) {
        this.source = source;
    }

    public String getDir() {
        return dir;
    }

    public void setDir(String dir) {
        this.dir = dir;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getOperatorName() {
        return operatorName;
    }

    public void setOperatorName(String operatorName) {
        this.operatorName = operatorName;
    }
}

Departure.java

 public class Departures {

        @SerializedName("all")
        @Expose
        private List<All> all = null;

        public List<All> getAll() {
            return all;
        }

        public void setAll(List<All> all) {
            this.all = all;
        }

    }

Call to the api

public void getBusList(String atcocode) {
    ApiService service = RetrofitClient.getRetrofitInstance().create(ApiService.class);

    String API_KEY = mContext.getResources().getString(R.string.api_key);
    String APP_ID = mContext.getResources().getString(R.string.api_id);
    Call<Departures> call = service.getBusList(atcocode, APP_ID, API_KEY, "no");
    call.enqueue(new Callback<Departures>() {

        @Override
        public void onResponse(@NonNull Call<Departures> call, @NonNull Response<Departures> response) {

            mBusListView.populateBusInfo(response.body().getAll());
        }

        @Override
        public void onFailure(@NonNull Call<Departures> call, @NonNull Throwable t) {

        }
    });
}


@GET("bus/stop/{atcocode}/live.json")
Call<Departures> getBusList(@Path("atcocode") String atcocode, @Query("app_id") String appId, @Query("api_key") String apiKey, @Query("group") String group);

I'm getting correct response from the browser. But when called through retrofit library getting success response(200) but the body is null.

I'm getting correct response from the browser. But when called through retrofit library getting success response(200) but the body is null.

1

1 Answers

2
votes

This is because Departures is a child of another entity in the JSON.

Try to call the getBusList using another entity that contains Departures, like this one.

public class Foo {

   private Departures departures;

   // getters and setters

}

Call<Foo> getBusList...

This should parse the result.