我有一个问题,我需要从CheckWX.com API解析多个对象类型的响应。 (请参见https://api.checkwx.com/)。目前,我的代码中包含ScalarsConverterFactory
和GSONConverterFactory
。我的Gson转换器工作正常,没有错误。但是,当我收到错误消息时,解析似乎失败并且调用了onFailure。我需要报告所有成功的站点以及失败的站点。如何在翻新中处理此问题?
这是单个站点请求错误的响应:
{
"results":1,
"data":[
"KNZL Invalid Station ICAO"
]
}
这是对站请求的响应,没有错误:
{
"results":1,
"data":[
{
"icao":"KAJO",
"name":"Corona Municipal",
"observed":"03-11-2018 @ 22:56Z",
"raw_text":"KAJO 032256Z AUTO 28010KT 10SM CLR 31/07 A2991 RMK AO2 SLP135 T03060072 $",
"barometer":{
"hg":29.91,
"kpa":101.29000000000001,
"mb":1013.5
},
"clouds":[
{
"code":"CLR",
"text":"Clear skies",
"base_feet_agl":0,
"base_meters_agl":0
}
],
"dewpoint":{
"celsius":7,
"fahrenheit":45
},
"elevation":{
"feet":535,
"meters":163
},
"flight_category":"VFR",
"humidity_percent":22,
"temperature":{
"celsius":31,
"fahrenheit":88
},
"visibility":{
"miles":"10",
"meters":"16,093"
},
"wind":{
"degrees":280,
"speed_kts":10,
"speed_mph":12,
"speed_mps":5
}
}
]
}
这是单站成功和单站错误的响应:
{
"results":2,
"data":[
{
"icao":"KAJO",
"name":"Corona Municipal",
"observed":"03-11-2018 @ 22:56Z",
"raw_text":"KAJO 032256Z AUTO 28010KT 10SM CLR 31/07 A2991 RMK AO2 SLP135 T03060072 $",
"barometer":{
"hg":29.91,
"kpa":101.29000000000001,
"mb":1013.5
},
"clouds":[
{
"code":"CLR",
"text":"Clear skies",
"base_feet_agl":0,
"base_meters_agl":0
}
],
"dewpoint":{
"celsius":7,
"fahrenheit":45
},
"elevation":{
"feet":535,
"meters":163
},
"flight_category":"VFR",
"humidity_percent":22,
"temperature":{
"celsius":31,
"fahrenheit":88
},
"visibility":{
"miles":"10",
"meters":"16,093"
},
"wind":{
"degrees":280,
"speed_kts":10,
"speed_mph":12,
"speed_mps":5
}
},
"CYPG METAR Currently Unavailable"
]
}
这是多个站点成功和错误的响应:
{
"results":5,
"data":[
{
"icao":"KAJO",
"name":"Corona Municipal",
"observed":"03-11-2018 @ 22:56Z",
"raw_text":"KAJO 032256Z AUTO 28010KT 10SM CLR 31/07 A2991 RMK AO2 SLP135 T03060072 $",
"barometer":{
"hg":29.91,
"kpa":101.29000000000001,
"mb":1013.5
},
"clouds":[
{
"code":"CLR",
"text":"Clear skies",
"base_feet_agl":0,
"base_meters_agl":0
}
],
"dewpoint":{
"celsius":7,
"fahrenheit":45
},
"elevation":{
"feet":535,
"meters":163
},
"flight_category":"VFR",
"humidity_percent":22,
"temperature":{
"celsius":31,
"fahrenheit":88
},
"visibility":{
"miles":"10",
"meters":"16,093"
},
"wind":{
"degrees":280,
"speed_kts":10,
"speed_mph":12,
"speed_mps":5
}
},
{
"icao":"KBIL",
"name":"Billings Logan International",
"observed":"03-11-2018 @ 22:53Z",
"raw_text":"KBIL 032253Z 25006KT 10SM -RA BKN043 BKN050 OVC120 05/01 A3007 RMK AO2 RAB41 SLP196 P0000 T00500011",
"barometer":{
"hg":30.07,
"kpa":101.83,
"mb":1019.6
},
"ceiling":{
"code":"BKN",
"text":"Broken",
"feet_agl":4300,
"meters_agl":1310.6400000000001
},
"clouds":[
{
"code":"BKN",
"text":"Broken",
"base_feet_agl":4300,
"base_meters_agl":1310.6400000000001
},
{
"code":"BKN",
"text":"Broken",
"base_feet_agl":5000,
"base_meters_agl":1524
},
{
"code":"OVC",
"text":"Overcast",
"base_feet_agl":12000,
"base_meters_agl":3657.6000000000004
}
],
"conditions":[
{
"code":"RA",
"prefix":"-",
"text":"Light Rain"
}
],
"dewpoint":{
"celsius":1,
"fahrenheit":34
},
"elevation":{
"feet":3579,
"meters":1091
},
"flight_category":"VFR",
"humidity_percent":76,
"rain_in":0.0050000000000000001,
"temperature":{
"celsius":5,
"fahrenheit":41
},
"visibility":{
"miles":"10",
"meters":"16,093"
},
"wind":{
"degrees":250,
"speed_kts":6,
"speed_mph":7,
"speed_mps":3
}
},
{
"icao":"KSAN",
"name":"San Diego International",
"observed":"03-11-2018 @ 22:51Z",
"raw_text":"KSAN 032251Z 29006KT 10SM FEW004 SCT250 24/16 A2993 RMK AO2 SLP135 T02390156",
"barometer":{
"hg":29.93,
"kpa":101.34999999999999,
"mb":1013.5
},
"clouds":[
{
"code":"FEW",
"text":"Few",
"base_feet_agl":400,
"base_meters_agl":121.92
},
{
"code":"SCT",
"text":"Scattered",
"base_feet_agl":25000,
"base_meters_agl":7620
}
],
"dewpoint":{
"celsius":16,
"fahrenheit":61
},
"elevation":{
"feet":39,
"meters":12
},
"flight_category":"VFR",
"humidity_percent":61,
"temperature":{
"celsius":24,
"fahrenheit":75
},
"visibility":{
"miles":"10",
"meters":"16,093"
},
"wind":{
"degrees":290,
"speed_kts":6,
"speed_mph":7,
"speed_mps":3
}
},
"CYPG METAR Currently Unavailable",
"KGNG METAR Currently Unavailable"
]
}
这是我的改装客户的代码:
retrofit = new Retrofit.Builder().baseUrl(baseUrl)
.client(getHttpClient())
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
正如我所说,我需要知道的是如何正确解析响应中的错误字符串:为澄清起见,这里是错误:
CheckWXApi: onFailure: java.lang.IllegalStateException: Expected
BEGIN_OBJECT but was STRING at line 1 column 755 path $.data[1],
java.lang.IllegalStateException: Expected BEGIN_OBJECT but was
STRING at line 1 column 755 path $.data[1]
2018-11-04 11:55:52.376 25533-25533/us.sensornet.aviationweatherv2
D/CheckWXApiProxy: onMetarReportFailure: called:
java.lang.IllegalStateException: Expected BEGIN_OBJECT but was
STRING at line 1 column 755 path $.data[1]
Caused BY: java.lang.IllegalStateException: Expected BEGIN_OBJECT
but was STRING at line 1 column 755 path $.data[1].
用于具有一个成功工作站和一个错误的查询。如果查看提供的响应,则会看到数据数组中返回的最后一个或两个对象是字符串错误消息,而不是metar weather对象。我的问题是正确处理这些字符串。仅仅添加ScalarConverterFactory并不能解决这个问题。