简体   繁体   中英

Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $?

When I call weather free API

https://api.apixu.com/v1/forecast.json?key=8f71b99b716f4278b98103446181912&q=Paris

Problems are

  1. My post-man and using Call in Retrofit I get data
  2. Using POJO class I get response error

ERROR Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

Call<List<DetailsMain>> listCall = api.getDetailsByParameter("8f71b99b716f4278b98103446181912", "Paris");

listCall.enqueue(new Callback<List<DetailsMain>>() {
        @Override
        public void onResponse(Call<List<DetailsMain>> call, Response<List<DetailsMain>> response) {
            DetailsMainList.setValue(response.body());
            Log.d("data", "" + response.body());
        }

        @Override
        public void onFailure(Call<List<DetailsMain>> call, Throwable t) {
            Log.d("Error", "" + t.getMessage());
        }
});

Check your POJO. Check if it is in accordance with the response that you are receiving. According to the error that you mentioned, you are getting a JsonObject in your response for which you have declared a JsonArray in your POJO.

You are Receiving List in response which means json start from jsonArray but response is start from Json Object so code must like

Call<DetailsMain> listCall = api.getDetailsByParameter("8f71b99b716f4278b98103446181912", "Paris");

when you enqueue the retrofit request you have to change List also there ( otherwise compiler shows error)

Note : DetailsMain is your pojo class which you can generate from any online tool / android studio plugin.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM