Android – Google Books API example

This is step-by-step instruction how to search books on Google Books and show them on GridView

Step 1: Add 2 permissions to AndroidManifest.xml:

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Step 2:Add Android Asynchronous Http Client library to project. Open build.gradle for your module(for example – app) and add following dependency to dependencies:

    compile ''

Step 3:Click right button of your mouse->New->package. Name that package: lazy list. Download and unzip it. copy four java files into that package.

Step 4:Copy following icon for book where there is no cover or not downloaded yet.

Step 5: Let’s create layout for list item:listitem_book.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
   android:orientation="vertical" android:layout_width="match_parent"


       android:text="Android Development"
       android:layout_gravity="center_horizontal" />

       android:text="Joe Richard"
       android:layout_gravity="center_horizontal" />


Step 6: Create list adapter for books list:

public class LibraryAdapter extends BaseAdapter {

    Context context;
    List<Book> booksList;

    public LibraryAdapter(Context context, List<Book> booksList) {
        this.context = context;
        this.booksList = booksList;

    public int getCount() {
        return booksList.size();

    public Object getItem(int position) {
        return booksList.get(position);

    public long getItemId(int position) {
        return position;

    public View getView(int position, View convertView, ViewGroup parent) {
        View view = parent.inflate(context, R.layout.listitem_book, null);

        Book book = (Book) getItem(position);

        TextView tvTitle = (TextView) view.findViewById(;

        TextView tvAuthor = (TextView) view.findViewById(;

        ImageView ivCover = (ImageView) view.findViewById(;
        ImageLoader imageLoader=new ImageLoader(context);
        imageLoader.DisplayImage(book.getImage_url(), ivCover);

        return view;

Step 7: newText – search word for book title. This piece of code searches books with ‘newText’ in its title.

                newText = newText.replace(" ", "+");
                String url = "";
                url = url + newText;

                AsyncHttpClient client = new AsyncHttpClient();
                client.get(url, new AsyncHttpResponseHandler() {
                    public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {

                        List<Book> bookList = new ArrayList<Book>();

                        String json = new String(responseBody);

                        try {
                            JSONObject object = new JSONObject(json);
                            JSONArray array = object.getJSONArray("items");

                            for (int i = 0; i < array.length(); i++) {
                                Book book = new Book();
                                JSONObject item = array.getJSONObject(i);

                                JSONObject volumeInfo = item.getJSONObject("volumeInfo");
                                String title = volumeInfo.getString("title");

                                JSONArray authors = volumeInfo.getJSONArray("authors");
                                String author = authors.getString(0);

                                JSONObject imageLinks = volumeInfo.getJSONObject("imageLinks");
                                String imageLink = imageLinks.getString("smallThumbnail");

                        } catch (JSONException e) {

                        LibraryFragment.showBooksList(getApplicationContext(), bookList);


                    public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {



That’s it! Let’s run your application!