我正在尝试从openweathermap api获取每小时预测数据使用此网址http://api.openweathermap.org/data/2.5/forecast?q=London&APPID使用改造库,但此网址无法正常工作..
Api接口类
public interface ApiInterface {
@GET("forecast?")
Call<HourlyWeather> getHourlyWeather(@Query("q") String city,
@Query("units") String units,
@Query("APPID") String appId);
}
api客户端类
public class ApiClient {
public static final String BASE_URL="http://api.openweathermap.org/data/2.5/";
public static Retrofit retrofit =null;
public static Retrofit getRetrofit(){
if (retrofit == null){
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
改装响应
ApiInterface apiInterface = ApiClient.getRetrofit().create(ApiInterface.class);
Call<HourlyWeather> call = apiInterface.getHourlyWeather("London","metric",getResources().getString(R.string.api_key));
call.enqueue(new Callback<HourlyWeather>() {
@Override
public void onResponse(Call<HourlyWeather> call, Response<HourlyWeather> response) {
Log.d("Hourly","Status code "+response.code());
list = response.body().getList();
hourlyTxt.setText(getString(R.string.hourly_forecast));
}
@Override
public void onFailure(Call<HourlyWeather> call, Throwable t) {
t.printStackTrace();
}
});
}
Logcat错误:
com.google.gson.JsonSyntaxException: java.lang.NumberFormatException: Expected an int but was 1035.16 at line 1 column 138 path $.list[0].main.pressure
03-25 18:41:17.789 16913-16913/com.example.prem.weatherapp W/System.err: at com.google.gson.internal.bind.TypeAdapters$7.read(TypeAdapters.java:245)
03-25 18:41:17.789 16913-16913/com.example.prem.weatherapp W/System.err: at com.google.gson.internal.bind.TypeAdapters$7.read(TypeAdapters.java:235)
03-25 18:41:17.789 16913-16913/com.example.prem.weatherapp W/System.err: at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:129)
03-25 18:41:17.789 16913-16913/com.example.prem.weatherapp W/System.err: at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220)
03-25 18:41:17.789 16913-16913/com.example.prem.weatherapp W/System.err: at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:129)
03-25 18:41:17.789 16913-16913/com.example.prem.weatherapp W/System.err: at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220)
03-25 18:41:17.789 16913-16913/com.example.prem.weatherapp W/System.err: at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)
03-25 18:41:17.789 16913-16913/com.example.prem.weatherapp W/System.err: at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82)
03-25 18:41:17.789 16913-16913/com.example.prem.weatherapp W/System.err: at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
03-25 18:41:17.789 16913-16913/com.example.prem.weatherapp W/System.err: at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:129)
03-25 18:41:17.789 16913-16913/com.example.prem.weatherapp W/System.err: at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220)
03-25 18:41:17.789 16913-16913/com.example.prem.weatherapp W/System.err: at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:37)
03-25 18:41:17.789 16913-16913/com.example.prem.weatherapp W/System.err: at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:25)
03-25 18:41:17.790 16913-16913/com.example.prem.weatherapp W/System.err: at retrofit2.ServiceMethod.toResponse(ServiceMethod.java:118)
03-25 18:41:17.790 16913-16913/com.example.prem.weatherapp W/System.err: at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:212)
03-25 18:41:17.790 16913-16913/com.example.prem.weatherapp W/System.err: at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:106)
03-25 18:41:17.790 16913-16913/com.example.prem.weatherapp W/System.err: at okhttp3.RealCall$AsyncCall.execute(RealCall.java:135)
03-25 18:41:17.790 16913-16913/com.example.prem.weatherapp W/System.err: at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
03-25 18:41:17.790 16913-16913/com.example.prem.weatherapp W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
03-25 18:41:17.790 16913-16913/com.example.prem.weatherapp W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
03-25 18:41:17.790 16913-16913/com.example.prem.weatherapp W/System.err: at java.lang.Thread.run(Thread.java:818)
03-25 18:41:17.790 16913-16913/com.example.prem.weatherapp W/System.err: Caused by: java.lang.NumberFormatException: Expected an int but was 1035.16 at line 1 column 138 path $.list[0].main.pressure
03-25 18:41:17.790 16913-16913/com.example.prem.weatherapp W/System.err: at com.google.gson.stream.JsonReader.nextInt(JsonReader.java:1196)
03-25 18:41:17.790 16913-16913/com.example.prem.weatherapp W/System.err: at com.google.gson.internal.bind.TypeAdapters$7.read(TypeAdapters.java:243)
03-25 18:41:17.790 16913-16913/com.example.prem.weatherapp W/System.err: ... 20 more
提前致谢...
答案 0 :(得分:0)
你期待一个int但是收到了main.pressure的双倍。
NumberFormatException: Expected an int but was 1035.16