itn*_*oob 5 api android google-books retrofit
我正在尝试将谷歌图书 API 与Retrofit一起使用,它返回一个空结果。
这是网址:https : //www.googleapis.com/books/v1/volumes? q
=9781451648546
在改造中,我有一个界面:
public interface IServiceEndPoint {
@GET("https://www.googleapis.com/books/v1/volumes?q=9781451648546")
Call<BookList> getBooks();
}
Run Code Online (Sandbox Code Playgroud)
在我webservice class我有以下方法:
public void getBooks(final Callback<BookList> callback){
IServiceEndPoint endPoint = mRetrofit.create(IServiceEndPoint.class);
Call<BookList> call = endPoint.getBooks();
call.enqueue(callback);
}
Run Code Online (Sandbox Code Playgroud)
在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) {
}
});
}
Run Code Online (Sandbox Code Playgroud)
我有一个 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;
}
}
Run Code Online (Sandbox Code Playgroud)
在我添加的清单文件中
使用权限 android:name="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;
}
}
Run Code Online (Sandbox Code Playgroud)
另外我创建了类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;
}
}
Run Code Online (Sandbox Code Playgroud)
感谢大家的帮助!
在您的 gradle 文件中添加两个依赖项。
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
Run Code Online (Sandbox Code Playgroud)
创建一个类,让我们使用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);
}
}
Run Code Online (Sandbox Code Playgroud)
现在你像这样声明你的界面
public interface IServiceEndPoint {
@GET("volumes")
Call<BookList> getBooks(@Query("q") String id);
}
Run Code Online (Sandbox Code Playgroud)
现在在 Activity 或 Fragment 中,以这种方式使用 Retrofit
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) {
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1634 次 |
| 最近记录: |