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 'com.loopj.android:android-async-http:1.4.8'

Step 3:Click right button of your mouse->New->package. Name that package: lazy list. Download
lazylist.zip 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.
stub

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical" android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:padding="17dp">

    <ImageView
       android:layout_width="75dp"
       android:layout_height="116dp"
       android:id="@+id/ivCover"
       android:src="@drawable/book1"
       android:layout_gravity="center_horizontal"
       android:background="#FF0000"/>

    <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:id="@+id/tvTitle"
       android:text="Android Development"
       android:textColor="#000000"
       android:layout_gravity="center_horizontal" />

    <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:id="@+id/tvAuthor"
       android:text="Joe Richard"
       android:textColor="#000000"
       android:layout_gravity="center_horizontal" />

</LinearLayout>

Step 6: Create list adapter for books list: LibraryAdapter.java

public class LibraryAdapter extends BaseAdapter {

    Context context;
    List<Book> booksList;

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

    @Override
    public int getCount() {
        return booksList.size();
    }

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

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    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(R.id.tvTitle);
        tvTitle.setText(book.getTitle());

        TextView tvAuthor = (TextView) view.findViewById(R.id.tvAuthor);
        tvAuthor.setText(book.getAuthor());

        ImageView ivCover = (ImageView) view.findViewById(R.id.ivCover);
        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.

            if(newText.length()>0)
            {
                newText = newText.replace(" ", "+");
                String url = "https://www.googleapis.com/books/v1/volumes?q=";
                url = url + newText;

                AsyncHttpClient client = new AsyncHttpClient();
                client.get(url, new AsyncHttpResponseHandler() {
                    @Override
                    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");
                                book.setTitle(title);

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

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

                                bookList.add(book);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        LibraryFragment.showBooksList(getApplicationContext(), bookList);

                    }

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

                    }

                });
            }

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