Rarity7-
Rarity7-

Reputation: 187

Retrofit2/GSON cannot convert to object

I'm making requets to Google Books API with Retrofit2 and use GSON converter, but have an error

ErrorExpected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

API URL to JSON

This is my

BookService.java

public class BookService{

    private static final String BASE_URL = "https://www.googleapis.com/";
    private BookApiService mApiService;
    private BookCallback mListener;

    public BookService(BookCallback listener){
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(BASE_URL)
                .build();

        mApiService = retrofit.create(BookApiService.class);

        mListener = listener;
    }



    public void getBooks(String query){
        final ApiResponse apiResponse = new ApiResponse();
        Call<List<Book>> call = mApiService.getBooks(query);
        call.enqueue(new Callback<List<Book>>() {
            @Override
            public void onResponse(Call<List<Book>> call, Response<List<Book>> response) {
                apiResponse.setBooks(response.body());
                mListener.notifyDataReceived(apiResponse);
            }

            @Override
            public void onFailure(Call<List<Book>> call, Throwable t) {
                apiResponse.setError(t);
                mListener.notifyDataReceived(apiResponse);
            }
        });
    }

    public interface BookCallback{
        void notifyDataReceived(ApiResponse apiResponse);
    }
}

and my Interface

BookApiService.java

public interface BookApiService {

    @GET("/books/v1/volumes")
    Call<List<Book>> getBooks(@Query("q") String query);
}

Then making request from MainActivity.java that implements BookCallback

public class MainActivity extends AppCompatActivity implements BookService.BookCallback {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main)
        new BookService(this).getBooks("android");
    }
}

Upvotes: 0

Views: 294

Answers (2)

Radesh
Radesh

Reputation: 13555

Your response is not an array. its an object with array in it.

For fix this you must create other response class like BookResponse

BookResponse.java

public class BookResponse{
      @SerializedName("kind")
      private Sting kind;
      @SerializedName("totalItems")
      private Int totalItems;
      @SerializedName("items")
      private List<Book> items;
}

And change your interface like this

public interface BookApiService {

    @GET("/books/v1/volumes")
    Call<BookResponse> getBooks(@Query("q") String query);
}

Upvotes: 2

apksherlock
apksherlock

Reputation: 8371

The response you are having is not an array of Books, but it is an object, which perhaps is a response that has an array with books. So what the error says: Your JsonResponse is not starting with [] but is starting with {}

Upvotes: 0

Related Questions