无法解析以下json数据。获得成功响应,但返回的正文为null以下是我通过网络浏览器获取的json响应。
{
"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"
}
下面是用于解析所使用的响应类的模型类
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;
}
}
调用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);
我从浏览器中得到了正确的答复。但是,当通过改造库调用时,获得成功响应(200),但主体为空。
我从浏览器中得到了正确的答复。但是,当通过改造库调用时,获得成功响应(200),但主体为空。
答案 0 :(得分:2)
这是因为Departures
是JSON中另一个实体的子代。
请尝试使用另一个包含getBusList
的实体来调用Departures
,像这样。
public class Foo {
private Departures departures;
// getters and setters
}
Call<Foo> getBusList...
这应该解析结果。