Android Open Source - Book-MetaSearch Goodreads 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;
/*w  w  w .  jav  a  2s  . co  m*/
import android.util.Log;

import net.grosinger.bookmetasearch.ApiKeys;
import net.grosinger.bookmetasearch.book.Author;
import net.grosinger.bookmetasearch.book.Book;

import org.w3c.dom.CharacterData;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import java.io.IOException;
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;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

/**
 * Created by tony on 11/2/13.
 */
public class GoodreadsQuery implements ProductQuery {
    private static final String BASE_URL = "http://www.goodreads.com/";
    private String query;

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

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

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

        List<Book> results = new ArrayList<Book>();
        try {
            String url = BASE_URL + "search.xml?key=" + ApiKeys.GOODREADS_ACCESS_KEY + "&q=" + URLEncoder.encode(query, "UTF-8");
            Document response = performGetRequest(url);
            if (response != null) {
                NodeList works = response.getDocumentElement().getElementsByTagName("work");
                for (int i = 0; i < works.getLength(); i++) {
                    Node node = works.item(i);

                    String authorName = "";
                    long authorId = 0;
                    String title = "";
                    String large_img = "";
                    float avg_rating = 0f;
                    String bookId = "";
                    String isbn = "";
                    String isbn13 = "";

                    if (node.getNodeType() == Node.ELEMENT_NODE) {
                        Element workElement = (Element) node;

                        Element authorNameElement = (Element) workElement.getElementsByTagName("name").item(0);
                        authorName = authorNameElement.getFirstChild().getNodeValue().trim();

                        Element titleElement = (Element) workElement.getElementsByTagName("title").item(0);
                        title = titleElement.getFirstChild().getNodeValue().trim();

                        Element imageElement = (Element) workElement.getElementsByTagName("image_url").item(0);
                        large_img = imageElement.getFirstChild().getNodeValue().trim();

                        Element bookIdElement = (Element) workElement.getElementsByTagName("id").item(1);
                        bookId = bookIdElement.getFirstChild().getNodeValue().trim();

                        Element authorIdElement = (Element) workElement.getElementsByTagName("id").item(2);
                        authorId = Long.parseLong(bookIdElement.getFirstChild().getNodeValue().trim());

                        Element ratingElement = (Element) workElement.getElementsByTagName("average_rating").item(0);
                        avg_rating = Float.parseFloat(ratingElement.getFirstChild().getNodeValue().trim());
                    }

                    String url2 = BASE_URL + "book/show?key=" + ApiKeys.GOODREADS_ACCESS_KEY + "&format=xml&id=" + bookId;
                    Document response2 = performGetRequest(url2);
                    if (response2 != null) {
                        Element bookElement = (Element) response2.getDocumentElement().getElementsByTagName("book").item(0);
                        Log.d(getClass().getSimpleName(), bookElement.getNodeName());


                        CharacterData isbnElement = (CharacterData) bookElement.getElementsByTagName("isbn").item(0).getFirstChild();
                        if (isbnElement != null) {
                            isbn = isbnElement.getData();
                        }

                        CharacterData isbn13Element = (CharacterData) bookElement.getElementsByTagName("isbn13").item(0).getFirstChild();
                        if (isbn13Element != null) {
                            isbn13 = isbn13Element.getData();
                        }
                    }

                    Author author = new Author.AuthorBuilder(authorId).setName(authorName).build();
                    Book book = new Book.BookBuilder(bookId, this).setTitle(title).setAvgRating(avg_rating).setLargeImg(large_img).setAuthor(author).setIsbn(isbn).setIsbn13(isbn13).build();
                    results.add(book);

                    Log.d(getClass().getSimpleName(), "Loaded book: " + book);
                }
            } else {
                Log.e(getClass().getSimpleName(), "Search failed");
            }
        } catch (UnsupportedEncodingException e) {
            Log.e(getClass().getSimpleName(), "Unsupported encoding exception", e);
        }

        return results;
    }

    private Document performGetRequest(String url) {
        Log.d(getClass().getSimpleName(), "Getting " + url);

        try {
            HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
            conn.setRequestMethod("GET");

            if (conn.getResponseCode() != 200) {
                return null;
            }

            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(conn.getInputStream());
            doc.getDocumentElement().normalize();

            conn.disconnect();
            return doc;

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }
        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