Search code examples
javaandroidretrofitgoogle-books

Android - Return a book's ID using Google BooksAPI


I am using Google Books API to display a list of various books by searching for terms, queries....

So far everything is working as it is supposed to be, one problem though, I need to get a book by it's id, I believe the problem is with my BookResponse class

UPDATE I figured I should add a Serialized id in BookItems class but I don't know where to go from there

PS. below you will find every class and a an image attached to display the JSON structure

Search in MainActivity

ApiInterface apiService =
            RetrofitInstance.getRetrofitInstance().create(ApiInterface.class);

    retrofit2.Call<BookResponce> call = apiService.getBooksById("zyTCAlFPjgYC", BOOKS_API_KEY);
    call.enqueue(new Callback<BookResponce>() {
        @Override
        public void onResponse(retrofit2.Call<BookResponce> call, Response<BookResponce> response) {
            bookResp = response.body();
            for (int i = 0; i == 0; i++){
                bookList.add(i, bookResp.getBooks().get(i).getBookData());
            }

            cAdapter.notifyDataSetChanged();
        }

        @Override
        public void onFailure(retrofit2.Call<BookResponce> call, Throwable t) {
            Toast.makeText(this, "failed", Toast.LENGTH_LONG).show();
        }
    });

BookResponse class

public class BookResponce {
    @SerializedName(value="items")
    ArrayList<BookItems> bookItems;

    @SerializedName(value="totalItems")
    int totalItems;

    public int getTotalItems() { return totalItems; }
    public ArrayList<BookItems> getBooks() { return bookItems; }
}

BookItems class

public class BookItems {
    //This is what I added
    @SerializedName(value = "id")
    String id;

    @SerializedName(value="volumeInfo")
    Book bookData;

    //and this...
    public String getId() {
        return id;
    }

    public Book getBookData(){return bookData; }
}

API Interface class

public interface ApiInterface {

    @GET("volumes/{id}")
    Call<BookResponce> getBooksById(@Path("id") String bookId, 
        @Query("API_KEY") String apiKey);

    @GET("volumes")
    Call<BookResponce> getBooksByQuery(@Query("q") String query, 
        @Query("API_KEY") String apiKey, @Query("maxResults") int maxResults);
}

Solution

  • when ever you're performing a search, after adding items in your bookList just add:

    bookList.get(i).setBookId(bookResp.getBooks().get(i).getId());

    Here's the full example in your case:

    ApiInterface apiService =
            RetrofitInstance.getRetrofitInstance().create(ApiInterface.class);
    
    retrofit2.Call<BookResponce> call = apiService.getBooksById("zyTCAlFPjgYC", BOOKS_API_KEY);
    call.enqueue(new Callback<BookResponce>() {
        @Override
        public void onResponse(retrofit2.Call<BookResponce> call, Response<BookResponce> response) {
            bookResp = response.body();
            for (int i = 0; i == 0; i++){
                bookList.add(i, bookResp.getBooks().get(i).getBookData());
    
                //This is what you must add
                bookList.get(i).setBookId(bookResp.getBooks().get(i).getId());
            }
    
            cAdapter.notifyDataSetChanged();
        }
    
        @Override
        public void onFailure(retrofit2.Call<BookResponce> call, Throwable t) {
            Toast.makeText(this, "failed", Toast.LENGTH_LONG).show();
        }
    });
    

    If anyone has a more optimal solution is welcome to edit/add his!