我试图了解改造是如何工作的,但每次我的回复都失败了我甚至使用了GitHub API,但我的回复仍然失败了,我做错了什么 我很困惑
链接
http://api.openweathermap.org/data/2.5/weather?q=Spakane,Us&appid=api_key
接口类
@GET("data/2.5/weather")
Call<List<Weather>> getWeatherData(@Query("q") String cityName,@Query("appid") String apikey);
MainActivity
private final String BASE_URL = "http://api.openweathermap.org/";
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
RestApi api = retrofit.create(RestApi.class);
final Call<List<Weather>> weather = api.getWeatherData("rawalpindi",API_KEY);
final ProgressDialog pg = new ProgressDialog(this);
pg.setTitle("Downloading");
pg.setMessage("Please Wait .....");
pg.show();
weather.enqueue(new Callback<List<Weather>>() {
@Override
public void onResponse(Call<List<Weather>> call, Response<List<Weather>> response) {
pg.dismiss();
Log.d(TAG, "onResponse: "+response.isSuccessful());
Log.d(TAG, "onResponse: "+response.body().get(0).getClouds());
}
@Override
public void onFailure(Call<List<Weather>> call, Throwable t) {
Log.d(TAG, "onFailure: Failed :(");
Log.d(TAG, "onResponseFailed: "+call.request().url());
pg.dismiss();
}
});
日志
D/MainActivity: onFailure: Failed :(
D/MainActivity: onResponseFailed: http://api.openweathermap.org/data/2.5/weather?q=rawalpindi&appid=api_key
抛出消息
Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
答案 0 :(得分:1)
你的答案不是一个清单,只是一个对象。使用拦截器查看回复。
作为示例,来自Call<List<Weather>>
,您应该只使用Call<Weather>
。
答案 1 :(得分:-2)
print throwable error,例如“Log.e(TAG,”NET_ERROR:“+ t.toString());”。这可能是网络问题。