我开发了一个应用程序,我在其中使用 googleapibook 搜索来获取书籍。我有一个 isbn 号。现在我想让我的用户逐页阅读那本书。但我没有找到任何方法或解决方案来阅读 java 或 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();
}
}
}