使用高德地图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,就能找出问题所在