Retorfit2とMoshiを用いてlivedoorの天気APIを利用して、天気情報を取得したいです。以下のコードを実行したところ以下のエラーがエミュレーターに表示され、取得できません。どこを直せばよいか教えてください。
expected begin_array but was begin_object at path $
どこを直せばよいか教えてください。
以下のJSONオブジェクトを変換させようとしています。
JSON
1{ 2 "pinpointLocations": [ 3 { 4 "link": "http://weather.livedoor.com/area/forecast/2710000", 5 "name": "大阪市" 6 }, 7 { 8 "link": "http://weather.livedoor.com/area/forecast/2714000", 9 "name": "堺市" 10 }, 11 { 12 "link": "http://weather.livedoor.com/area/forecast/2720200", 13 "name": "岸和田市" 14 }, 15 { 16 "link": "http://weather.livedoor.com/area/forecast/2720300", 17 "name": "豊中市" 18 }, 19 { 20 "link": "http://weather.livedoor.com/area/forecast/2720400", 21 "name": "池田市" 22 }, 23 { 24 "link": "http://weather.livedoor.com/area/forecast/2720500", 25 "name": "吹田市" 26 }, 27 { 28 "link": "http://weather.livedoor.com/area/forecast/2720600", 29 "name": "泉大津市" 30 }, 31 { 32 "link": "http://weather.livedoor.com/area/forecast/2720700", 33 "name": "高槻市" 34 }, 35 { 36 "link": "http://weather.livedoor.com/area/forecast/2720800", 37 "name": "貝塚市" 38 }, 39 { 40 "link": "http://weather.livedoor.com/area/forecast/2720900", 41 "name": "守口市" 42 }, 43 { 44 "link": "http://weather.livedoor.com/area/forecast/2721000", 45 "name": "枚方市" 46 }, 47 { 48 "link": "http://weather.livedoor.com/area/forecast/2721100", 49 "name": "茨木市" 50 }, 51 { 52 "link": "http://weather.livedoor.com/area/forecast/2721200", 53 "name": "八尾市" 54 }, 55 { 56 "link": "http://weather.livedoor.com/area/forecast/2721300", 57 "name": "泉佐野市" 58 }, 59 { 60 "link": "http://weather.livedoor.com/area/forecast/2721400", 61 "name": "富田林市" 62 }, 63 { 64 "link": "http://weather.livedoor.com/area/forecast/2721500", 65 "name": "寝屋川市" 66 }, 67 { 68 "link": "http://weather.livedoor.com/area/forecast/2721600", 69 "name": "河内長野市" 70 }, 71 { 72 "link": "http://weather.livedoor.com/area/forecast/2721700", 73 "name": "松原市" 74 }, 75 { 76 "link": "http://weather.livedoor.com/area/forecast/2721800", 77 "name": "大東市" 78 }, 79 { 80 "link": "http://weather.livedoor.com/area/forecast/2721900", 81 "name": "和泉市" 82 }, 83 { 84 "link": "http://weather.livedoor.com/area/forecast/2722000", 85 "name": "箕面市" 86 }, 87 { 88 "link": "http://weather.livedoor.com/area/forecast/2722100", 89 "name": "柏原市" 90 }, 91 { 92 "link": "http://weather.livedoor.com/area/forecast/2722200", 93 "name": "羽曳野市" 94 }, 95 { 96 "link": "http://weather.livedoor.com/area/forecast/2722300", 97 "name": "門真市" 98 }, 99 { 100 "link": "http://weather.livedoor.com/area/forecast/2722400", 101 "name": "摂津市" 102 }, 103 { 104 "link": "http://weather.livedoor.com/area/forecast/2722500", 105 "name": "高石市" 106 }, 107 { 108 "link": "http://weather.livedoor.com/area/forecast/2722600", 109 "name": "藤井寺市" 110 }, 111 { 112 "link": "http://weather.livedoor.com/area/forecast/2722700", 113 "name": "東大阪市" 114 }, 115 { 116 "link": "http://weather.livedoor.com/area/forecast/2722800", 117 "name": "泉南市" 118 }, 119 { 120 "link": "http://weather.livedoor.com/area/forecast/2722900", 121 "name": "四條畷市" 122 }, 123 { 124 "link": "http://weather.livedoor.com/area/forecast/2723000", 125 "name": "交野市" 126 }, 127 { 128 "link": "http://weather.livedoor.com/area/forecast/2723100", 129 "name": "大阪狭山市" 130 }, 131 { 132 "link": "http://weather.livedoor.com/area/forecast/2723200", 133 "name": "阪南市" 134 }, 135 { 136 "link": "http://weather.livedoor.com/area/forecast/2730100", 137 "name": "島本町" 138 }, 139 { 140 "link": "http://weather.livedoor.com/area/forecast/2732100", 141 "name": "豊能町" 142 }, 143 { 144 "link": "http://weather.livedoor.com/area/forecast/2732200", 145 "name": "能勢町" 146 }, 147 { 148 "link": "http://weather.livedoor.com/area/forecast/2734100", 149 "name": "忠岡町" 150 }, 151 { 152 "link": "http://weather.livedoor.com/area/forecast/2736100", 153 "name": "熊取町" 154 }, 155 { 156 "link": "http://weather.livedoor.com/area/forecast/2736200", 157 "name": "田尻町" 158 }, 159 { 160 "link": "http://weather.livedoor.com/area/forecast/2736600", 161 "name": "岬町" 162 }, 163 { 164 "link": "http://weather.livedoor.com/area/forecast/2738100", 165 "name": "太子町" 166 }, 167 { 168 "link": "http://weather.livedoor.com/area/forecast/2738200", 169 "name": "河南町" 170 }, 171 { 172 "link": "http://weather.livedoor.com/area/forecast/2738300", 173 "name": "千早赤阪村" 174 } 175 ], 176 "link": "http://weather.livedoor.com/area/forecast/270000", 177 "forecasts": [ 178 { 179 "dateLabel": "今日", 180 "telop": "雨時々曇", 181 "date": "2020-03-28", 182 "temperature": { 183 "min": null, 184 "max": { 185 "celsius": "14", 186 "fahrenheit": "57.2" 187 } 188 }, 189 "image": { 190 "width": 50, 191 "url": "http://weather.livedoor.com/img/icon/17.gif", 192 "title": "雨時々曇", 193 "height": 31 194 } 195 }, 196 { 197 "dateLabel": "明日", 198 "telop": "曇時々雨", 199 "date": "2020-03-29", 200 "temperature": { 201 "min": { 202 "celsius": "8", 203 "fahrenheit": "46.4" 204 }, 205 "max": { 206 "celsius": "15", 207 "fahrenheit": "59.0" 208 } 209 }, 210 "image": { 211 "width": 50, 212 "url": "http://weather.livedoor.com/img/icon/10.gif", 213 "title": "曇時々雨", 214 "height": 31 215 } 216 } 217 ], 218 "location": { 219 "city": "大阪", 220 "area": "近畿", 221 "prefecture": "大阪府" 222 }, 223 "publicTime": "2020-03-28T05:00:00+0900", 224 "copyright": { 225 "provider": [ 226 { 227 "link": "http://tenki.jp/", 228 "name": "日本気象協会" 229 } 230 ], 231 "link": "http://weather.livedoor.com/", 232 "title": "(C) LINE Corporation", 233 "image": { 234 "width": 118, 235 "link": "http://weather.livedoor.com/", 236 "url": "http://weather.livedoor.com/img/cmn/livedoor.gif", 237 "title": "livedoor 天気情報", 238 "height": 26 239 } 240 }, 241 "title": "大阪府 大阪 の天気", 242 "description": { 243 "text": " 大阪府は、前線や湿った空気の影響で雨となっています。\n\n 28日の大阪府は、前線や湿った空気の影響で雨となり、朝から昼過ぎまでは曇るでしょう。\n\n 29日の大阪府は、前線や湿った空気の影響でおおむね曇り、明け方まで雨が降る見込みです。\n\n【近畿地方】\n 近畿地方は、前線や湿った空気の影響で曇り、雨の降っている所があります。\n\n 28日の近畿地方は、前線や湿った空気の影響で曇り、夕方から雨が降るでしょう。\n\n 29日の近畿地方は、前線や湿った空気の影響でおおむね曇り、朝まで雨が降る見込みです。", 244 "publicTime": "2020-03-28T04:47:00+0900" 245 } 246}
data class は以下のようになり、WeatherPropertyに詰めてやりとりを行うとし、それぞれのdata classは独立させてます。
Kotlin
1data class WeatherProperty( 2 val copyright: Copyright, 3 val description: Description, 4 val forecasts: Forecast, 5 val link: String, 6 val location: Location, 7 val pinpointLocations: PinpointLocation, 8 val publicTime: String, 9 val title: String 10)
Kotlin
1data class Copyright( 2 val provider: Provider, 3 val link: String, 4 val title: String, 5 val image: Image 6)
Kotlin
1data class Provider( 2 val providerlist: List<ProviderList> 3)
Kotlin
1data class ProviderList( 2 val link: String, 3 val name: String 4)
Kotlin
1data class Description( 2 val publicTime: String, 3 val text: String 4)
Kotlin
1 2data class Forecast( 3 val forecastList: List<ForecastList> 4)
Kotlin
1data class ForecastList( 2 val date: String, 3 val dateLabel: String, 4 val image: ImageX, 5 val telop: String, 6 val temperature: Temperature 7)
Kotlin
1data class ImageX( 2 val height: Int, 3 val title: String, 4 val url: String, 5 val width: Int 6)
Kotlin
1data class Temperature( 2 val max: Max, 3 val min: Any 4)
Kotlin
1data class Location( 2 val area: String, 3 val city: String, 4 val prefecture: String 5)
Kotlin
1data class PinpointLocation( 2 val linkName: List<LinkName> 3)
Kotlin
1data class LinkName( 2 val link: String, 3 val name: String 4)
取得してきたJSONオブジェクトを変換するAPIは以下のようにしています。
Kotlin
1private const val BASE_URL = "http://weather.livedoor.com/forecast/webservice/json/" 2 3 4private val moshi = Moshi.Builder() 5 .add(KotlinJsonAdapterFactory()) 6 .build() 7 8//retrofitオブジェクトをMoshi使ってMoshiオブジェクトに変換 9private val retrofit = Retrofit.Builder() 10 .addConverterFactory(MoshiConverterFactory.create(moshi)) 11 .addCallAdapterFactory(CoroutineCallAdapterFactory()) 12 .baseUrl(BASE_URL) 13 .build() 14 15interface WeatherApiService { 16 @GET("v1?city=270000") 17 fun getProperties(): 18 Deferred<List<WeatherProperty>> 19} 20 21object WeatherApi { 22 val retrofitService : WeatherApiService by lazy { 23 retrofit.create(WeatherApiService::class.java) 24 } 25}
以上よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー