Android Open Source - Book-MetaSearch Google Query






From Project

Back to project page Book-MetaSearch.

License

The source code is released under:

Apache License

If you think the Android project Book-MetaSearch listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package net.grosinger.bookmetasearch.loader;
//www.  j a v a  2s  .  c  om
import android.util.Log;

import net.grosinger.bookmetasearch.book.Author;
import net.grosinger.bookmetasearch.inventory.AvailableBook;
import net.grosinger.bookmetasearch.book.Book;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Tony on 11/7/13.
 */
public class GoogleQuery implements ProductQuery, InventoryQuery {
    private String query;
    private Book targetBook;

    @Override
    public void setSearchTarget(Book book) {
        Log.d(getClass().getSimpleName(), "Setting target: " + book);
        targetBook = book;
    }

    @Override
    public void setSearchTerms(String searchTerms) {
        Log.d(getClass().getSimpleName(), "Setting query: " + searchTerms);
        this.query = searchTerms;
    }

    @Override
    public List<AvailableBook> loadInventory() {
        Log.d(getClass().getSimpleName(), "Loading in background: " + targetBook);

        if (targetBook == null) {
            return null;
        }

        // TODO: Get inventory from Google
        return null;
    }

    @Override
    public List<Book> loadProducts() {
        Log.d(getClass().getSimpleName(), "Loading in background: " + query);

        if (query == null || query == "") {
            return null;
        }

        try {
            String searchTerm = URLEncoder.encode(query, "UTF-8");
            String url = "https://www.googleapis.com/books/v1/volumes?q=" + searchTerm;
            return responseToBooks(requestJson(url));
        } catch (UnsupportedEncodingException e) {
            Log.e(getClass().getSimpleName(), "Unable to URL encode search terms", e);
        }
        return null;
    }

    private List<Book> responseToBooks(JSONObject results) {
        List<Book> books = new ArrayList<Book>();
        JSONArray items = null;
        try {
            items = results.getJSONArray("items");
        } catch (JSONException e) {
            Log.e(getClass().getSimpleName(), "Could not retrieve items from json, aborting", e);
        }
        for (int i = 0; i < items.length(); i++) {
            try {
                JSONObject item = items.getJSONObject(i);
                JSONObject volumeInfo = item.getJSONObject("volumeInfo");

                Book.BookBuilder builder = new Book.BookBuilder(item.getString("id"), this);
                builder.setTitle(volumeInfo.getString("title"));

                String author_name = volumeInfo.getJSONArray("authors").getString(0);
                Author author = new Author.AuthorBuilder(0).setName(author_name).build();
                builder.setAuthor(author);

                if(volumeInfo.has("publisher")) {
                    builder.setPublisher(volumeInfo.getString("publisher"));
                }
                if(volumeInfo.has("imageLinks") && volumeInfo.getJSONObject("imageLinks").has("thumbnail")) {
                    builder.setLargeImg(volumeInfo.getJSONObject("imageLinks").getString("thumbnail"));
                }
                if(volumeInfo.has("imageLinks") && volumeInfo.getJSONObject("imageLinks").has("smallThumbnail")) {
                    builder.setSmallImg(volumeInfo.getJSONObject("imageLinks").getString("smallThumbnail"));
                }
                if(volumeInfo.has("averageRating")) {
                    builder.setAvgRating(volumeInfo.getDouble("averageRating"));
                }
                if(volumeInfo.has("description")) {
                    builder.setDescription(volumeInfo.getString("description"));
                }
                if(volumeInfo.has("pageCount")) {
                    builder.setNumPages(volumeInfo.getInt("pageCount"));
                }

                JSONArray isbnNumbers = volumeInfo.getJSONArray("industryIdentifiers");
                for (int j = 0; j < isbnNumbers.length(); j++) {
                    JSONObject isbnItem = isbnNumbers.getJSONObject(j);
                    if (isbnItem.getString("type").equals("ISBN_10")) {
                        builder.setIsbn(isbnItem.getString("identifier"));
                    } else {
                        builder.setIsbn13(isbnItem.getString("identifier"));
                    }
                }
                books.add(builder.build());
            } catch (JSONException e) {
                Log.e(getClass().getSimpleName(), "Invalid action on json, skipping book", e);
            }
        }

        return books;
    }

    private JSONObject requestJson(String url) {
        try {
            URL obj = new URL(url);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();
            int responseCode = con.getResponseCode();

            if (responseCode == 200) {
                InputStream in = con.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"), 8);
                StringBuilder sb = new StringBuilder();

                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }

                return new JSONObject(sb.toString());
            }
        } catch (MalformedURLException e) {
            Log.e(getClass().getSimpleName(), "Invalid URL, aborting search", e);
        } catch (UnsupportedEncodingException e) {
            Log.e(getClass().getSimpleName(), "Invalid encoding, aborting search", e);
        } catch (JSONException e) {
            Log.e(getClass().getSimpleName(), "Invalid JSON or non-existent attribute, aborting", e);
        } catch (IOException e) {
            Log.e(getClass().getSimpleName(), "IOException, aborting search", e);
        }
        return null;
    }
}




Java Source Code List

com.ECS.client.jax.Accessories.java
com.ECS.client.jax.Arguments.java
com.ECS.client.jax.BrowseNode.java
com.ECS.client.jax.BrowseNodes.java
com.ECS.client.jax.Collections.java
com.ECS.client.jax.CustomerReviews.java
com.ECS.client.jax.DecimalWithUnits.java
com.ECS.client.jax.EditorialReview.java
com.ECS.client.jax.EditorialReviews.java
com.ECS.client.jax.Errors.java
com.ECS.client.jax.ImageSet.java
com.ECS.client.jax.Image.java
com.ECS.client.jax.ItemAttributes.java
com.ECS.client.jax.ItemLink.java
com.ECS.client.jax.ItemLinks.java
com.ECS.client.jax.Item.java
com.ECS.client.jax.LoyaltyPoints.java
com.ECS.client.jax.Merchant.java
com.ECS.client.jax.NewReleases.java
com.ECS.client.jax.NonNegativeIntegerWithUnits.java
com.ECS.client.jax.OfferAttributes.java
com.ECS.client.jax.OfferListing.java
com.ECS.client.jax.OfferSummary.java
com.ECS.client.jax.Offer.java
com.ECS.client.jax.Offers.java
com.ECS.client.jax.Price.java
com.ECS.client.jax.Promotion.java
com.ECS.client.jax.Promotions.java
com.ECS.client.jax.Property.java
com.ECS.client.jax.RelatedItem.java
com.ECS.client.jax.RelatedItems.java
com.ECS.client.jax.SimilarProducts.java
com.ECS.client.jax.TopItemSet.java
com.ECS.client.jax.TopSellers.java
com.ECS.client.jax.Tracks.java
com.ECS.client.jax.VariationAttribute.java
com.ECS.client.jax.VariationDimensions.java
com.ECS.client.jax.VariationSummary.java
com.ECS.client.jax.Variations.java
net.grosinger.bookmetasearch.BookDetail.java
net.grosinger.bookmetasearch.BookInventoryAdapter.java
net.grosinger.bookmetasearch.Home.java
net.grosinger.bookmetasearch.SearchActivity.java
net.grosinger.bookmetasearch.book.Author.java
net.grosinger.bookmetasearch.book.Book.java
net.grosinger.bookmetasearch.book.provider.BookProvider.java
net.grosinger.bookmetasearch.book.provider.LibraryBookProvider.java
net.grosinger.bookmetasearch.book.provider.RetailBookProvider.java
net.grosinger.bookmetasearch.fragment.DetailFragment.java
net.grosinger.bookmetasearch.fragment.HomeFragment.java
net.grosinger.bookmetasearch.fragment.SearchResultsFragment.java
net.grosinger.bookmetasearch.inventory.AvailableBook.java
net.grosinger.bookmetasearch.inventory.InventoryHeader.java
net.grosinger.bookmetasearch.inventory.InventoryListItem.java
net.grosinger.bookmetasearch.loader.AmazonQuery.java
net.grosinger.bookmetasearch.loader.AsyncImageLoader.java
net.grosinger.bookmetasearch.loader.GoodreadsQuery.java
net.grosinger.bookmetasearch.loader.GoogleQuery.java
net.grosinger.bookmetasearch.loader.InventoryLoader.java
net.grosinger.bookmetasearch.loader.InventoryQuery.java
net.grosinger.bookmetasearch.loader.ProductLoader.java
net.grosinger.bookmetasearch.loader.ProductQuery.java
net.grosinger.bookmetasearch.search.RecentBookSearchSuggestionsProvider.java
net.grosinger.bookmetasearch.search.SearchResultAdapter.java