解决java.lang.IllegalStateException: Expected a string but was BEGIN_ARRAY at XXX

使用高德地图api请求数据报错:

Caused by: java.lang.IllegalStateException: Expected a string but was BEGIN_ARRAY at line 1 column 127 path $.regeocode.addressComponent.province

data class是用jsontokotlin插件生成的大概率没有问题,所以看了下请求接口发现

@GET("v3/geocode/regeo?key=${SunnyWeatherApplication.GeoKey}")
fun getDistrict(@Query("location") location: Location): Call<DistrictResponse>
data class Location(val lng: String, val lat: String) //lng经度, lat纬度

此时url内拼接的location变成了location=Location(lng=114.11, lat=22.33),然后服务器返回了这样一串json数据,此时很多数据从String变成了[]数组,然后就报错BEGIN_ARRAY

{"status":"1","info":"OK","infocode":"10000","regeocode":{"formatted_address":[],"addressComponent":{"country":[],"province":[],"city":[],"citycode":[],"district":[],"adcode":[],"township":[],"towncode":[],"streetNumber":{"street":[],"number":[],"location":"114.24689922.720968","direction":[],"distance":[]}},"pois":[],"roads":[],"roadinters":[],"aois":[]}}

其实应该拼接的是location=114.11,22.33

@GET("v3/geocode/regeo?key=${SunnyWeatherApplication.GeoKey}")
fun getDistrict(@Query("location") location: String): Call<DistrictResponse>
SunnyWeatherNetwork.getDistrict("$lng,$lat")

修改后成功获取了数据

 

总结:BEGIN_ARRAY是解析出来的json数据格式不对,查看API文档、data class和拼接后的url,就能找出问题所在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值