Android Open Source - Book-MetaSearch Amazon 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;
/*from  www  .j a  va 2  s . co  m*/
import android.util.Log;

import com.ECS.client.jax.Item;
import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;

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

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by tony on 11/3/13.
 */
public class AmazonQuery implements InventoryQuery {
    private Book target;

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

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

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

        List<AvailableBook> books = null;
        if (target.getIsbn13() != null) {
            Log.d(getClass().getSimpleName(), "Searching for ISBN13 " + target.getIsbn13());
            books = performSearch(target.getIsbn13());
        }
        if ((books == null || books.size() == 0) && target.getIsbn() != null) {
            Log.d(getClass().getSimpleName(), "Searching for ISBN " + target.getIsbn());
            books = performSearch(target.getIsbn());
        }

        return books;
    }

    private List<AvailableBook> performSearch(String isbn) {
        Gson gson = new Gson();
        List<AvailableBook> books = new ArrayList<AvailableBook>();

        try {
            URL url = new URL("http://wiki.grosinger.net:8080/search?method=isbn&q=" + isbn);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("GET");

            int responseCode = con.getResponseCode();
            if (responseCode == 200) {
                JsonReader reader = new JsonReader(new InputStreamReader(con.getInputStream()));
                List<Item> items = new ArrayList<Item>();
                reader.beginArray();
                while (reader.hasNext()) {
                    Item results = gson.fromJson(reader, Item.class);
                    items.add(results);
                }
                reader.endArray();
                reader.close();

                for (Item item : items) {
                    books.add(new AvailableBook(target, AvailableBook.Format.EBOOK, item.getItemLinks().getItemLink().get(0).getURL(), "Amazon"));
                }
            } else {
                Log.e(getClass().getSimpleName(), "Unable to complete request");
            }
        } catch (MalformedURLException e) {
            Log.e(getClass().getSimpleName(), "Invalid URL", e);
            e.printStackTrace();
        } catch (IOException e) {
            Log.e(getClass().getSimpleName(), "IOException", e);
        }
        return books;
    }
}




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