Android-API响应返回OK,但列表为空

时间:2018-11-19 19:26:49

标签: java android api retrofit2 google-books

我正在使用Google Books APIrecyclerview中显示有关书籍的相关信息(例如书名,作者,出版商...),并且为此我正在使用Retrofit2工作正常,状态代码为200(根据Google的文档,这表示确定),但是包含书籍的列表为空,您将在下面找到每个相关类的代码

从API接口开始,该接口包括通过id搜索书籍的方法:

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Path;
import retrofit2.http.Query;

public interface ApiInterface {
    @GET("volumes/{id}")
    Call<BookResponce> getBooks(@Path("id") String id, @Query("API_KEY") String apiKey);
}

这是我正在使用的Retrofit实例类:

import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitInstance {
    private static Retrofit retrofit;
    private static final String BASE_URL = "https://www.googleapis.com/books/v1/";

    public static Retrofit getRetrofitInstance(){
        if (retrofit == null){
            retrofit = new retrofit2.Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        }
        return retrofit;
    }
}

下面是Book.java模型类

@SerializedName("id")
private String bookId;

@SerializedName("description")
private String bookDescription;

@SerializedName("title")
private String bookTitle;

@SerializedName("authors")
private List<String> bookAuthors = new ArrayList<>();

@SerializedName("averageRating")
private float bookRating;

现在,这是图书回复类

@SerializedName("kind")
private String bkind;

@SerializedName("items")
private List<Book> results;

@SerializedName("totalItems")
private int totalItems;

最后是Main Activity类

public static final String BOOKS_API_KEY = "...";
private List<Book> bookList = new ArrayList<>();
private RecyclerView rv;
private BookAdapter mAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    rv = (RecyclerView) findViewById(R.id.recycler_a);

    mAdapter = new BookAdapter(bookList);

    rv.setLayoutManager(new LinearLayoutManager(this));
    rv.setAdapter(mAdapter);

    DisplayBook();
}

private void DisplayBook(){
    ApiInterface apiService =
            RetrofitInstance.getRetrofitInstance().create(ApiInterface.class);

    retrofit2.Call<BookResponce> call = apiService.getBooks("zyTCAlFPjgYC", BOOKS_API_KEY);
    call.enqueue(new Callback<BookResponce>() {
        @Override
        public void onResponse(retrofit2.Call<BookResponce> call, Response<BookResponce> response) {
            int statusCode = response.code();
            int totalItems = response.body().getTotalItems();
            List<Book> books = response.body().getResults();
            bookList = books;
            mAdapter.notifyDataSetChanged();

            Toast.makeText(getApplicationContext(), "code: "+statusCode+" | total "+totalItems, Toast.LENGTH_LONG).show();
        }

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

请记住这里的主要问题:

STATUS CODE = 200 (Success)
but
TOTAL ITEMS RETURNED = 0 /** List<Book> books = response.body().getResults(); */

0 个答案:

没有答案