简体   繁体   中英

google books api retrofit

I am trying to use the google books API with Retrofit , it is returning an empty result.

this is the url:
https://www.googleapis.com/books/v1/volumes?q=9781451648546

In Retrofit I have an interface:

public interface IServiceEndPoint {
  @GET("https://www.googleapis.com/books/v1/volumes?q=9781451648546")
  Call<BookList> getBooks();
}

in my webservice class I have the following method:

public void getBooks(final Callback<BookList> callback){
  IServiceEndPoint endPoint = mRetrofit.create(IServiceEndPoint.class);
  Call<BookList> call = endPoint.getBooks();
  call.enqueue(callback);
}

in the activity class I have the method:

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) {

    }
  });
}

I have a Java class Book and 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;
    }
}

In the manifest file I added

uses-permission android:name="android.permission.INTERNET

mBooks is returning null value, how could I solve this?
Thank you.

EDIT: shuvro 's answer helped me correcting the problem. I also forgot to include the volumeInfo in my Book class. My book class looks as followed now:

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;
    }

}

Additionally I created the class 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;
    }
}

Thanks all for the help!

Add the two dependency in your gradle file .

compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'

Create a class , lets sats ServiceGenerator , your class should be like this

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);
    }
}

Now your declare your interface like this

public interface IServiceEndPoint {
  @GET("volumes")
  Call<BookList> getBooks(@Query("q") String id);
}

Now in activity or in fragment , use retrofit in this way

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) {

        }
    });

I would go on this way and simply follow the way to do usually with retrofit.

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;
  }

}

and where you want to use it :

   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) {

        }
    });

And BookList, you got the idea I guess

public class BookList {

String kind;
int totalItems;
List<Book> items;
...
}

(off course adapt with your own code)

Also be sure to have added the internet permission. You can follow this because there is no reasons to not success calling the api. just be sure also your field name are correct and match the one contained in the JSON returned.

You BookList POJO class has nothing to do with JSON response. It should be something like that:

public class BookList { 
    @SerializedName("items")
    private List<Item> items = new ArrayList<Item>();
}

You can find all POJO classes for that response here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM