2011-05-19 81 views
1

我開發了一個應用程序,使用googleapibook搜索獲取圖書。我有一個isbn沒有。現在我想讓我的用戶逐頁閱讀那本書。但我沒有找到任何方法或解決方案來閱讀在Java或Android的書。在Android中閱讀谷歌圖書

這是我的代碼。

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.getString(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(); 
     } 

    } 
} 
+2

我想這只是爲了獲得知識,因爲有一個谷歌圖書 – yeradis 2011-05-19 11:26:17

+0

的官方應用程序,我知道哥們..但我怎樣才能查看或閱讀谷歌書...你有什麼想法? – 2011-05-19 11:28:14

+0

Maibe如果您使用webview和Embedded Viewer API,可能您將能夠獲得該書的預覽 – yeradis 2011-05-19 11:36:18

回答

1

這裏有一個可能的解決方案。

當你正在使用的圖書搜索API,你能夠獲得書號的書

然後,讓你的用戶閱讀這本書,也許:

使用的WebView與谷歌文檔中內嵌的瀏覽器API +您的ISBN,你將能夠加載的WebView

例如,使用此代碼網頁視圖內的圖書預覽:

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

這是我的解決方案,技巧位於:google.books.DefaultViewer(document.getElementById('viewerCanvas')); viewer.load('ISBN:0738531367');

我希望這可以幫助你

對這個比較瞭解,請訪問http://code.google.com/apis/books/docs/viewer/developers_guide.html的「你好,世界」的嵌入式瀏覽器API

的當您使用API​​,你可以得到類似這個:http://code.google.com/apis/books/docs/viewer/examples/book-simple.html

+0

Hi Nirav您是否能夠解決您的問題?我也在尋找一些關於如何在Android應用中閱讀谷歌書籍的示例代碼。請如果你有解決方案提供一些示例代碼或分享你的想法。謝謝。 – 2017-07-08 01:02:28