开发者

Read google book in android

i have developed an application in which i fetch books using googleapibook search. I have an isbn no. Now i want to let my user read that book page by page. But i didn't find any method or solution to read book in java or android.

Here is my code.

package com.project.bookhunt;

import java.net.URL;
import java.util.ArrayList;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.widget.Toast;

public class BookSearchParser 
{
    Context m_context;
    static ArrayList<Book_Item> books= new ArrayList<Book_Item>();
    String searchResult;
    BookSearchParser(Context c,URL url)
    {
        try
        {

           m_context=c;
           HttpClient client = new HttpClient();

           GetMethod getMethod = new GetMethod(url.toString());
           int statusCode = client.executeMethod(getMethod);
           System.out.println("status Code"+statusCode);
           System.out.println(getMethod.getResponseBodyAsString());
           searchResult=getMethod.getResponseBodyAsString();
           parseJSON(searchResult);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    public static ArrayList<Book_Item> getBooks()
    {
        return books;
    }

    private void parseJSON(String object)
     {
         try
         {
         books=new ArrayList<Book_Item>();
         JSONObject jSonObject = new JSONObject(object);
         if(jSonObject.getInt("totalItems")>0)
         {
             JSONArray jSonObjectArray = jSonObject.getJSONArray("items");
             for(int count = 0; count < jSonObjectArray.length();count++)
             {  
                    Book_Item item=new Book_Item();
                    JSONObject jsonItem = (JSONObject) jSonObjectArray.get(count);

                    if(jsonItem.has(("id")))
                    {
                        item.setId(jsonItem.getString("id"));   
                    }
//                  else
//                  {
//                      item.setId("No Id");
//                  }


                    if(jsonItem.has("selfLink"))
                    {
                      item.setSelfLink(jsonItem.getString("selfLink"));

                    }
//                  else
//                  {
//                      item.setSelfLink("No Link Avilable");
//                  }



                        if(jsonItem.has("volumeInfo"))
                        {
                             JSONObject volumeInfo = (JSONObject)jsonItem.get("volumeInfo");

                                if(volumeInfo.has("title"))
                                {
                                    item.setTitle(volumeInfo.getString("title"));
                                }
//                              else
//                              {
//                                  item.setTitle("No Title");
//                              }


                                if(volumeInfo.has("subtitle"))
                                {
                                    item.setSubTitle(volumeInfo.getString("subtitle"));
                                }
//                              else
//                              {
//                                  item.setSubTitle("No SubTitle Avilable");
//                              }
//                              
                                    if(volumeInfo.has("authors"))
                                    {
                                         JSONArray Authors = volumeInfo.getJSONArray("authors");
                                            for(int authorCount=0;authorCount<Authors.length();authorCount++)
                                            {
                                                    item.setAuthor(Authors.getString(authorCount));         
                                            }
                                    }



                                  if(volumeInfo.has("description"))
                                  {
                                      item.setDiscription(volumeInfo.getString("description"));
                                  }
//                                else
//                                {
//                                    item.setDiscription("No Description Avilable");
//                                }

                                  if(volumeInfo.has("averageRating"))
                                  {
                                      item.setRating(volumeInfo.getString("averageRating"));
                                  }


                                  if(volumeInfo.has("industryIdentifiers"))
                                  { 
                                      JSONArray isbnArray = volumeInfo.getJSONArray("industryIdentifiers");


                                            for(int isbnCount=0;isbnCount<isbnArray.length();isbnCount++)
                                            {
                                                JSONObject isbn=(JSONObject)isbnArray.get(isbnCount);
                                                if(isbn.getString(("type")).equals("ISBN_10"))
                                                {
                                                    item.setIsbn10(isbn.getString("identifier"));
                                                }
                                                if(isbn.getString(("type")).equals("ISBN_13"))
                                                {
                                                    item.setIsbn13(isbn.getString("identifier"));
                                                }

                                            }

                                  }

                                if(volumeInfo.has("categories"))
                                {
                                      JSONArray categoriesArray = volumeInfo.getJSONArray("categories");

                                        for(int j=0;j<categoriesArray.length();j++)
                                        {
                                            item.setCategory(categoriesArray.ge开发者_JS百科tString(j));                 
                                        }
                                }
//                              else
//                              {
//                                  item.setCategory("No category");
//                              }
//                              


                                if(volumeInfo.has("imageLinks"))
                                {
                                    JSONObject ImageLinks = volumeInfo.getJSONObject("imageLinks");

                                    if(ImageLinks.has("smallThumbnail"))
                                    {
                                        item.setSmallThumb(ImageLinks.getString("smallThumbnail"));
                                    }
                                    if(ImageLinks.has("thumbnail"))
                                    {
                                        item.setThumb(ImageLinks.getString("thumbnail"));
                                    }
//                                  else
//                                  {
//                                      //item.setSmallThumb("No Thumbnail");
//                                  }
//                                  
                                    if(ImageLinks.has("previewLink"))
                                    {
                                        item.setPreviewLink((ImageLinks.getString("previewLink")));
                                    }
//                                  else
//                                  {
//                                      item.setPreviewLink("No Thumbnail");
//                                  }



                                }
//                              else
//                              {
//                                  //item.setSmallThumb("No Thumbnail");
//                                  item.setPreviewLink("No Preview");
//                              }
                        }


                        books.add(item);//add one volume to array_list
              }
         }
         else
         {
             Toast.makeText(m_context, "0 Record Found..",Toast.LENGTH_SHORT).show();
         }











         }
         catch(Exception e)
         {
             e.printStackTrace();
         }

     }
}


Here with a possible solution.

As you are using the Book Search API and you are able to get the ISBN of the book

Then, to allow your user to read the book , maybe:

Using a WebView with the Google Docs Embedded Viewer API + your ISBN you will be able to load the book preview inside that WebView

for example, a WebView with this code:

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>Google Books Embedded Viewer API Example</title>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load("books", "0");

      function initialize() {
        var viewer = new google.books.DefaultViewer(document.getElementById('viewerCanvas'));
        viewer.load('**ISBN:0738531367**');
      }

      google.setOnLoadCallback(initialize);
    </script>
  </head>
  <body>
    <div id="viewerCanvas" style="width: 600px; height: 500px"></div>
  </body>
</html>

This is my solution for you, the trick is located at : google.books.DefaultViewer(document.getElementById('viewerCanvas')); viewer.load('ISBN:0738531367');

I hope this helps you

for a better understanding on this please visit http://code.google.com/apis/books/docs/viewer/developers_guide.html at The "Hello, World" of the Embedded Viewer API

When you use that API you can get something like this: http://code.google.com/apis/books/docs/viewer/examples/book-simple.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜