Android - Return 一本书的 ID 使用 Google BooksAPI
Android - Return a book's ID using Google BooksAPI
我正在使用 Google 书籍 API 通过搜索术语、查询来显示各种书籍的列表....
到目前为止一切正常,但有一个问题,我需要通过它的 id 得到一本书,我相信问题出在我的 BookResponse
class
UPDATE 我想我应该在 BookItems
class 中添加一个序列化 ID,但我不知道从那里去哪里
PS。下面你会发现每个 class 和一张显示 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();
}
});
图书回复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 接口 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);
}
当您执行搜索时,在 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();
}
});
如果谁有更优的方案欢迎edit/add他的!
我正在使用 Google 书籍 API 通过搜索术语、查询来显示各种书籍的列表....
到目前为止一切正常,但有一个问题,我需要通过它的 id 得到一本书,我相信问题出在我的 BookResponse
class
UPDATE 我想我应该在 BookItems
class 中添加一个序列化 ID,但我不知道从那里去哪里
PS。下面你会发现每个 class 和一张显示 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();
}
});
图书回复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 接口 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);
}
当您执行搜索时,在 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();
}
});
如果谁有更优的方案欢迎edit/add他的!