-1

I am trying to understand how retrofit works but every time my response is failed I even used GitHub API but still my response failed, am I doing something wrong I am so confused

Link

http://api.openweathermap.org/data/2.5/weather?q=Spakane,Us&appid=api_key

Interface Class

 @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();
            }
        });

Log

D/MainActivity: onFailure: Failed :(
D/MainActivity: onResponseFailed: http://api.openweathermap.org/data/2.5/weather?q=rawalpindi&appid=api_key

throwable message

Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
Salman500
  • 1,213
  • 1
  • 17
  • 35
  • print throwable error message to get through the problem.. – Aalap Patel Aug 17 '17 at 02:25
  • this is the throwable message ""Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $"" – Salman500 Aug 17 '17 at 02:28
  • Possible duplicate of [Retrofit 2 Android expected BEGIN ARRAY but was BEGIN OBJECT at line 1 column 2](https://stackoverflow.com/questions/36177629/retrofit2-android-expected-begin-array-but-was-begin-object-at-line-1-column-2) – David Rawson Aug 17 '17 at 02:40
  • Possible duplicate of [Retrofit2 Android: Expected BEGIN\_ARRAY but was BEGIN\_OBJECT at line 1 column 2 path $](https://stackoverflow.com/questions/36177629/retrofit2-android-expected-begin-array-but-was-begin-object-at-line-1-column-2) – Cătălin Florescu Aug 17 '17 at 07:51

2 Answers2

2

Your answer is not a list, is just an object. Use an interceptor to see responses.

As an example, from Call<List<Weather>> you should use just Call<Weather>.

Cătălin Florescu
  • 5,012
  • 1
  • 25
  • 36
-2

print throwable error,such as "Log.e(TAG, "NET_ERROR:" + t.toString());". It may be the network problem .

Mr.Four
  • 67
  • 10