[英]Retrofit and RxJava cannot serialize Json Data properly
我收到了如下所示的API的响应,
{
"stop-schedule": {
"stop": {
"key": 10064,
"name": "Northbound Osborne at Glasgow",
"number": 10064,
"direction": "Northbound",
"side": "Nearside",
"street": {
"key": 2715,
"name": "Osborne Street",
"type": "Street"
},
"cross-street": {
"key": 1486,
"name": "Glasgow Avenue",
"type": "Avenue"
},
"centre": {
"utm": {
"zone": "14U",
"x": 633838,
"y": 5525742
},
"geographic": {
"latitude": "49.86912",
"longitude": "-97.1375"
}
}
},
"route-schedules": [
{
"route": {
"key": 16,
"number": 16,
"name": "Route 16 Selkirk-Osborne",
"customer-type": "regular",
"coverage": "regular"
},
"scheduled-stops": [
{
"key": "8131173-23",
"times": {
"arrival": {
"scheduled": "2018-02-02T23:07:20",
"estimated": "2018-02-02T23:07:20"
},
"departure": {
"scheduled": "2018-02-02T23:07:20",
"estimated": "2018-02-02T23:07:20"
}
},
"variant": {
"key": "16-0-B",
"name": "Selkirk-Osborne to Tyndall Park via Burrows"
},
"bus": {
"bike-rack": "false",
"easy-access": "true",
"wifi": "false"
}
},
{
"key": "8131174-24",
"times": {
"arrival": {
"scheduled": "2018-02-02T23:32:20",
"estimated": "2018-02-02T23:32:20"
},
"departure": {
"scheduled": "2018-02-02T23:32:20",
"estimated": "2018-02-02T23:32:20"
}
},
"variant": {
"key": "16-0-M",
"name": "Selkirk-Osborne to Tyndall Park via Manitoba"
},
"bus": {
"bike-rack": "false",
"easy-access": "true",
"wifi": "false"
}
},
{
"key": "8131175-23",
"times": {
"arrival": {
"scheduled": "2018-02-02T23:57:20",
"estimated": "2018-02-02T23:57:20"
},
"departure": {
"scheduled": "2018-02-02T23:57:20",
"estimated": "2018-02-02T23:57:20"
}
},
"variant": {
"key": "16-0-B",
"name": "Selkirk-Osborne to Tyndall Park via Burrows"
},
"bus": {
"bike-rack": "false",
"easy-access": "true",
"wifi": "false"
}
},
{
"key": "8131176-24",
"times": {
"arrival": {
"scheduled": "2018-02-03T00:22:20",
"estimated": "2018-02-03T00:22:20"
},
"departure": {
"scheduled": "2018-02-03T00:22:20",
"estimated": "2018-02-03T00:22:20"
}
},
"variant": {
"key": "16-0-M",
"name": "Selkirk-Osborne to Tyndall Park via Manitoba"
},
"bus": {
"bike-rack": "false",
"easy-access": "true",
"wifi": "false"
}
}
]
}
]
},
"query-time": "2018-02-02T22:48:55"
}
我正在使用RxJava方法进行改造以发出请求。 但是,Gson似乎无法识别数据的嵌套部分并将其放入适当的模型中。
Retrofit retrofit= new Retrofit.Builder()
.baseUrl(API_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
我的模特看起来像这样
public class StopSchedule implements Serializable {
@SerializedName("query-time")
private String mQueryTime;
@SerializedName("route-schedules")
@Expose
private List<RouteSchedule> mRouteSchedules;
@SerializedName("stop")
private Stop mStop;
@SerializedName("stop-schedule")
private StopSchedule mStopSchedule;
public String getQueryTime() {
return mQueryTime;
}
public List<RouteSchedule> getRouteSchedules() {
return mRouteSchedules;
}
public Stop getStop() {
return mStop;
}
public StopSchedule getStopSchedule() {
return mStopSchedule;
}
public static class Builder {
private String mQueryTime;
private List<RouteSchedule> mRouteSchedules;
private Stop mStop;
private StopSchedule mStopSchedule;
public StopSchedule.Builder withQueryTime(String queryTime) {
mQueryTime = queryTime;
return this;
}
public StopSchedule.Builder withRouteSchedules(List<RouteSchedule> routeSchedules) {
mRouteSchedules = routeSchedules;
return this;
}
public StopSchedule.Builder withStop(Stop stop) {
mStop = stop;
return this;
}
public StopSchedule.Builder withStopSchedule(StopSchedule stopSchedule) {
mStopSchedule = stopSchedule;
return this;
}
public StopSchedule build() {
StopSchedule StopSchedule = new StopSchedule();
StopSchedule.mQueryTime = mQueryTime;
StopSchedule.mRouteSchedules = mRouteSchedules;
StopSchedule.mStop = mStop;
StopSchedule.mStopSchedule = mStopSchedule;
return StopSchedule;
}
}
}
我也有RouteSchedule
类模型和其他类模型。 但是,使用单个改装进行正常响应后,我的RouteSchedule模型为空。
有没有一种方法可以让我单独获取路由计划数组,或者确保它不会返回空模型。
你的问题在这里。
@Expose
private List<RouteSchedule> mRouteSchedules;
@Expose
用于使用与JSON不同的相同字段名称。
所以删除@Expose
。
@SerializedName("route-schedules")
private List<RouteSchedule> mRouteSchedules;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.