改造响应失败
我想了解每一个我的回答是失败的时候,我是如何工作的改进,但即使使用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 $
您的回答不是清单,只是一个对象。使用拦截器来查看响应。
作为示例,从Call<List<Weather>>
您应该只使用Call<Weather>
。
ty回应我想出了自己 – Salman500
@ Salman500很高兴。对你有好处! –
打印抛出错误,如 “Log.e(TAG, ”NET_ERROR:“ + t.toString());”。这可能是网络问题。
可抛出的消息“Expected BEGIN_ARRAY but is BEGIN_OBJECT at line 1 column 2 path $” – Salman500
可能是List的日期类型
打印抛出错误消息通过问题得到.. –
这是抛出的消息“”预期BEGIN_ARRAY但BEGIN_OBJECT位于第1个第2列路径$“” – Salman500
的[2改造Android的预期BEGIN ARRAY可能重复但BEGIN OBJECT在第1行第2列](https://stackoverflow.com/questions/36177629/retrofit2-android-expected-begin-array-but-was-begin-object-at-line-1-column-2) –