【问题标题】:Android - Return a book's ID using Google BooksAPIAndroid - 使用 Google BooksAPI 返回一本书的 ID
【发布时间】:2018-12-01 16:06:17
【问题描述】:

我正在使用 Google Books API 通过搜索术语、查询来显示各种书籍的列表......

到目前为止,一切都按预期进行,但有一个问题,我需要通过它的 id 来买一本书,我相信问题出在我的 BookResponse 班级

更新我想我应该在 BookItems 类中添加一个序列化 id,但我不知道从那里去哪里

PS。下面你会发现每个类和一个附加的图像来显示 JSON 结构

在 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 类

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 类

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 接口类

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

【问题讨论】:

    标签: java android retrofit google-books


    【解决方案1】:

    当您执行搜索时,在您的 bookList 添加项目后,只需添加:

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

    这是您案例的完整示例:

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

    如果有人有更优化的解决方案,欢迎编辑/添加他的!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-06
      • 1970-01-01
      • 2021-08-14
      • 1970-01-01
      • 2019-03-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多