【发布时间】: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