我正在尝试使用带有Retrofit的Google图书API,它返回一个空结果。
这是网址:
https://www.googleapis.com/books/v1/volumes?q=9781451648546
在Retrofit中我有一个界面:
public interface IServiceEndPoint {
@GET("https://www.googleapis.com/books/v1/volumes?q=9781451648546")
Call<BookList> getBooks();
}
在我的webservice class
我有以下方法:
public void getBooks(final Callback<BookList> callback){
IServiceEndPoint endPoint = mRetrofit.create(IServiceEndPoint.class);
Call<BookList> call = endPoint.getBooks();
call.enqueue(callback);
}
在activity class
我有方法:
private void getBooks(){
WebserviceHelper.getmInstance().getBooks(new Callback<BookList>() {
@Override
public void onResponse(Call<BookList> call, Response<BookList> response) {
mBooks = response.body().getResults();
mBookAdapter.update(mBooks);
}
@Override
public void onFailure(Call<BookList> call, Throwable t) {
}
});
}
我有一个Java类 Book 和 BookList 。
public class Book implements Serializable {
@SerializedName("id")
private String id;
@SerializedName("title")
private String title;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
public class BookList extends Book implements Serializable {
@SerializedName("results")
private List<Book> results;
public List<Book> getResults() {
return results;
}
public void setResults(List<Book> results) {
this.results = results;
}
}
在清单文件中我添加了
uses-permission android:name =&#34; android.permission.INTERNET
mBooks 正在返回空值,我该如何解决这个问题呢? 谢谢。
编辑: shuvro 的回答帮助我纠正了问题。我也忘了在我的Book类中包含volumeInfo。我的书课现在看起来如下:
public class Book implements Serializable {
@SerializedName("id")
private String id;
private VolumeInfo volumeInfo;
public VolumeInfo getVolumeInfo() {
return volumeInfo;
}
public void setVolumeInfo(VolumeInfo volumeInfo) {
this.volumeInfo = volumeInfo;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
另外我创建了类volumeInfo:
public class VolumeInfo {
private String title;
private String subtitle;
private String publisher;
private String description;
public String getSubtitle() {
return subtitle;
}
public void setSubtitle(String subtitle) {
this.subtitle = subtitle;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
谢谢大家的帮助!
答案 0 :(得分:2)
在gradle文件中添加两个依赖项。
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
创建一个类,让我们坐在ServiceGenerator中,你的类应该是这样的
public class ServiceGenerator {
private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
private static Retrofit.Builder builder =
new Retrofit.Builder()
.baseUrl("https://www.googleapis.com/books/v1/")
.addConverterFactory(GsonConverterFactory.create());
public static <S> S createService(Class<S> serviceClass) {
Retrofit retrofit = builder.client(httpClient.build()).build();
return retrofit.create(serviceClass);
}
}
现在你声明你的界面
public interface IServiceEndPoint {
@GET("volumes")
Call<BookList> getBooks(@Query("q") String id);
}
现在处于活动或片段中,以这种方式使用改造
IServiceEndPoint serviceEndPoint = ServiceGenerator.createService(IServiceEndPoint.class)
Call<BookList> call = serviceEndPoint.getBooks("9781451648546");
call.enqueue(new Callback<BookList>() {
@Override
public void onResponse(Call<BookList> call, Response<BookList> response) {
//do whatever you want to do
}
@Override
public void onFailure(Call<BookList> call, Throwable t) {
}
});
答案 1 :(得分:1)
我会继续这样做,只需按照通常的方式进行改造。
public interface GBookService {
@GET("volumes?q=9781451648546")
Call<BookList> getBooks();
}
//
public class ApiHelper {
GBookService service;
public ApiHelper(){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://www.googleapis.com/books/v1/")
.build();
service = retrofit.create(GBookService.class);
}
public GBookService getService(){
return service;
}
}
以及您想要使用它的位置:
Call<BookList> call = apiHelper.getService().getBooks();
call.enqueue(new Callback<BookList>() {
@Override
public void onResponse(Call<BookList> call, Response<BookList> response) {
}
@Override
public void onFailure(Call<BookList> call, Throwable t) {
}
});
和BookList,你有了我的想法
public class BookList {
String kind;
int totalItems;
List<Book> items;
...
}
(当然适应你自己的代码)
另外请务必添加互联网权限。 您可以关注this,因为没有理由不成功调用api。请确保您的字段名称正确并且与返回的JSON中包含的名称相匹配。
答案 2 :(得分:1)
您BookList
POJO类与JSON响应无关。它应该是这样的:
public class BookList {
@SerializedName("items")
private List<Item> items = new ArrayList<Item>();
}
您可以找到该回复的所有POJO类here。