Search code examples
androidgithubretrofitretrofit2github-api

Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ in GitHub API


I am using GitHub API to fetch some data by the following URL. But was getting error Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $, though I have checked some related answer but can not solve my issue. So it would be great if anyone can help me out.

https://api.github.com/search/users?q=tom+repos:%3E10+followers:%3E100

@GET
Call<List<UserBase>> getTrendingDevelopers(@Url String url, @Query("q") String q);

Callback:

final Call<List<UserBase>> trendingDevelopersCall = apiService.getTrendingDevelopers("users?",url);
    trendingDevelopersCall.enqueue(new Callback<List<UserBase>>() {
        @Override
        public void onResponse(Call<List<UserBase>> call, Response<List<UserBase>> response) {
            if (response.isSuccessful()){
                if (response.body() != null){
                    if (response.body().size() > 0){
                        trendingDevelopers.setValue(response.body().get(0).getItems());
                    }
                }
            }
        }

        @Override
        public void onFailure(Call<List<UserBase>> call, Throwable t) {
            trendingDevelopers.setValue(null);
        }
    });

Solution

  • You are getting this error is because you are expecting response to be an object but in actual getting a list.

    In this @GET Call<List> getTrendingDevelopers(@Url String url, @Query("q") String q); function you are telling that your response is a list but it is not. You need to create a relevant class for API's response.