03-18 16:06:32.653 22906-22920/com.landleaf.mvvm D/OkHttp: Request weather info with error: undefined method `city' for nil:NilClass
03-18 16:06:32.653 22906-22920/com.landleaf.mvvm D/OkHttp: <-- END HTTP (73-byte body)
03-18 16:06:32.653 22906-22906/com.landleaf.mvvm D/MainActivity: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $
一毛一样的接口用retrofit自带Gson解析报错,用okhttp请求到数据再gson解析解析到了,百度到的一堆全都是说json不规范的,各种抓狂见鬼
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://guolin.tech/")
.client(okHttpClient.build())
.addConverterFactory(GsonConverterFactory.create())
.build();
WeatherService weatherService = retrofit.create(WeatherService.class);
retrofit2.Call<HeWeather> call1 = weatherService.getWeather("CN101010100", "79a79674ca6149c0a6a0900eadf99576");
call1.enqueue(new retrofit2.Callback<HeWeather>() {
@Override
public void onResponse(retrofit2.Call<HeWeather> call, retrofit2.Response<HeWeather> response) {
Log.d("MainActivity", "response.body():" + response.body());
}
@Override
public void onFailure(retrofit2.Call<HeWeather> call, Throwable t) {
Log.d("MainActivity", t.toString());
}
});
Request build = new Request.Builder().url("http://guolin.tech/api/weather?cityid=CN101010100&key=79a79674ca6149c0a6a0900eadf99576").build();
Call call = okHttpClient.build().newCall(build);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.d("MainActivity", e.toString());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String string = response.body().string();
Gson gson = new Gson();
HeWeather heWeather = gson.fromJson(string, HeWeather.class);
Log.d("MainActivity", "heWeather:" + heWeather);
}
});
03-18 16:06:32.653 22906-22920/com.landleaf.mvvm D/OkHttp: <-- 200 OK http://guolin.tech/api/weather?cityId=CN101010100&key=79a79674ca6149c0a6a0900eadf99576 (341ms)
03-18 16:06:32.653 22906-22920/com.landleaf.mvvm D/OkHttp: Content-Type: text/plain; charset=utf-8
03-18 16:06:32.653 22906-22920/com.landleaf.mvvm D/OkHttp: Transfer-Encoding: chunked
03-18 16:06:32.653 22906-22920/com.landleaf.mvvm D/OkHttp: Connection: keep-alive
03-18 16:06:32.653 22906-22920/com.landleaf.mvvm D/OkHttp: Status: 200 OK
03-18 16:06:32.653 22906-22920/com.landleaf.mvvm D/OkHttp: Cache-Control: max-age=0, private, must-revalidate
03-18 16:06:32.653 22906-22920/com.landleaf.mvvm D/OkHttp: ETag: W/"6a54780a43f5555f36d3bafb6a9f47f6"
03-18 16:06:32.653 22906-22920/com.landleaf.mvvm D/OkHttp: X-Frame-Options: SAMEORIGIN
03-18 16:06:32.653 22906-22920/com.landleaf.mvvm D/OkHttp: X-XSS-Protection: 1; mode=block
03-18 16:06:32.653 22906-22920/com.landleaf.mvvm D/OkHttp: X-Content-Type-Options: nosniff
03-18 16:06:32.653 22906-22920/com.landleaf.mvvm D/OkHttp: X-Runtime: 0.216947
03-18 16:06:32.653 22906-22920/com.landleaf.mvvm D/OkHttp: X-Request-Id: ade9fb48-3cd6-40e0-9990-20da17e5ff07
03-18 16:06:32.653 22906-22920/com.landleaf.mvvm D/OkHttp: Date: Mon, 18 Mar 2019 08:06:28 GMT
03-18 16:06:32.653 22906-22920/com.landleaf.mvvm D/OkHttp: X-Powered-By: Phusion Passenger 5.3.3
03-18 16:06:32.653 22906-22920/com.landleaf.mvvm D/OkHttp: Server: nginx/1.14.0 + Phusion Passenger 5.3.3
03-18 16:06:32.653 22906-22920/com.landleaf.mvvm D/OkHttp: Request weather info with error: undefined method `city' for nil:NilClass
03-18 16:06:32.653 22906-22920/com.landleaf.mvvm D/OkHttp: <-- END HTTP (73-byte body)
03-18 16:06:32.653 22906-22906/com.landleaf.mvvm D/MainActivity: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $
03-18 16:06:32.873 22906-22921/com.landleaf.mvvm D/OkHttp: <-- 200 OK http://guolin.tech/api/weather?cityid=CN101010100&key=79a79674ca6149c0a6a0900eadf99576 (557ms)
03-18 16:06:32.873 22906-22921/com.landleaf.mvvm D/OkHttp: Content-Type: text/plain; charset=utf-8
03-18 16:06:32.873 22906-22921/com.landleaf.mvvm D/OkHttp: Transfer-Encoding: chunked
03-18 16:06:32.873 22906-22921/com.landleaf.mvvm D/OkHttp: Connection: keep-alive
03-18 16:06:32.873 22906-22921/com.landleaf.mvvm D/OkHttp: Status: 200 OK
03-18 16:06:32.873 22906-22921/com.landleaf.mvvm D/OkHttp: Cache-Control: max-age=0, private, must-revalidate
03-18 16:06:32.873 22906-22921/com.landleaf.mvvm D/OkHttp: ETag: W/"8932eefeb8627ff8f8d4e75ab3c0cfbd"
03-18 16:06:32.873 22906-22921/com.landleaf.mvvm D/OkHttp: X-Frame-Options: SAMEORIGIN
03-18 16:06:32.873 22906-22921/com.landleaf.mvvm D/OkHttp: X-XSS-Protection: 1; mode=block
03-18 16:06:32.873 22906-22921/com.landleaf.mvvm D/OkHttp: X-Content-Type-Options: nosniff
03-18 16:06:32.873 22906-22921/com.landleaf.mvvm D/OkHttp: X-Runtime: 0.427339
03-18 16:06:32.873 22906-22921/com.landleaf.mvvm D/OkHttp: X-Request-Id: ddfd2018-ee68-44ee-b9c5-dc2eecfccd1f
03-18 16:06:32.873 22906-22921/com.landleaf.mvvm D/OkHttp: Date: Mon, 18 Mar 2019 08:06:28 GMT
03-18 16:06:32.873 22906-22921/com.landleaf.mvvm D/OkHttp: X-Powered-By: Phusion Passenger 5.3.3
03-18 16:06:32.873 22906-22921/com.landleaf.mvvm D/OkHttp: Server: nginx/1.14.0 + Phusion Passenger 5.3.3
03-18 16:06:32.873 22906-22921/com.landleaf.mvvm D/OkHttp: {"HeWeather": [{"basic":{"cid":"CN101010100","location":"北京","parent_city":"北京","admin_area":"北京","cnty":"**","lat":"39.90498734","lon":"116.4052887","tz":"+8.00","city":"北京","id":"CN101010100","update":{"loc":"2019-03-18 15:55","utc":"2019-03-18 07:55"}},"update":{"loc":"2019-03-18 15:55","utc":"2019-03-18 07:55"},"status":"ok","now":{"cloud":"0","cond_code":"101","cond_txt":"多云","fl":"19","hum":"25","pcpn":"0.0","pres":"1007","tmp":"21","vis":"8","wind_deg":"186","wind_dir":"南风","wind_sc":"2","wind_spd":"6","cond":{"code":"101","txt":"多云"}},"daily_forecast":[{"date":"2019-03-18","cond":{"txt_d":"多云"},"tmp":{"max":"21","min":"6"}},{"date":"2019-03-19","cond":{"txt_d":"多云"},"tmp":{"max":"24","min":"11"}},{"date":"2019-03-20","cond":{"txt_d":"小雨"},"tmp":{"max":"16","min":"6"}}],"aqi":{"city":{"aqi":"148","pm25":"113","qlty":"轻度污染"}},"suggestion":{"comf":{"type":"comf","brf":"舒适","txt":"白天不太热也不太冷,风力不大,相信您在这样的天气条件下,应会感到比较清爽和舒适。"},"sport":{"type":"sport","brf":"适宜","txt":"天气较好,赶快投身大自然参与户外运动,尽情感受运动的快乐吧。"},"cw":{"type":"cw","brf":"较适宜","txt":"较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。"}}}]}
03-18 16:06:32.873 22906-22921/com.landleaf.mvvm D/OkHttp: <-- END HTTP (1364-byte body)
03-18 16:06:32.883 22906-22921/com.landleaf.mvvm D/MainActivity: heWeather:HeWeather{list=[Weather{status='ok', basic=Basic{cityName='北京', weatherId='CN101010100', update=Update{updateTime='2019-03-18 15:55'}}, aqi=AQI{city=AQICity{aqi='148', pm25='113'}}, now=Now{temperature='21', more=More{info='多云'}}, suggestion=Suggestion{comfort=Comfort{info='白天不太热也不太冷,风力不大,相信您在这样的天气条件下,应会感到比较清爽和舒适。'}, carWash=CarWash{info='较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。'}, sport=Sport{info='天气较好,赶快投身大自然参与户外运动,尽情感受运动的快乐吧。'}}, forecastList=[Forecast{date='2019-03-18', temperature=Temperature{max='21', min='6'}, more=More{info='多云'}}, Forecast{date='2019-03-19', temperature=Temperature{max='24', min='11'}, more=More{info='多云'}}, Forecast{date='2019-03-20', temperature=Temperature{max='16', min='6'}, more=More{info='小雨'}}]}]}