How we created Lowprice Android app
[Guest post from Pranay Airan who’s an early adopter of DataWeave. Pranay is enthusiastic about app development. Below he shares his experiences of developing LowPrice, an Android app powered by DataWeave’s pricing API.]
In this two part blog post, I will discuss how I created LowPrice using DataWeave’s APIs and a custom backend. LowPrice (https://play.google.com/store/apps/details?id=com.binarybricks.lowprice) is an Android app that gets you the lowest price for books across 9 online stores in India simply by scanning the book’s barcode. LowPrice also lets you search books by title, author, or publisher, so you don’t even have to go to a store really!
In Part 1, let’s look at the relevant APIs provided by DataWeave, understand how to query data, and how to consume this data through an Android app. In Part 2, we will see how I created the Scan and Search features and how you can achieve the same. Finally, we will wire the code from these two posts to create a complete app.
DataWeave API and JSON
DataWeave provides APIs around public data available on the web. One of their APIs is around providing prices of books across various ecommerce stores in India. LowPrice uses this API http://www.dataweave.in/apis/dataset-Book-Price-Search-By-ISBN—19.html. This API covers a lot of different sources including some of the top ecommerce stores in India like Landmark, Infibeam, and HomeShop18.
For an exclusive access to their APIs, register on http://dataweave.in/ and get an access key. This gives you access to all the data APIs provided by DataWeave.
Lets look at the API for book prices:
1. Search by ISBN
This feature enables you to query for the price of a book across various stores by ISBN number of the book. The API call for the same is http://api.dataweave.in/v1/book_search/searchByIsbn/?isbn=9788174369109 . The API returns data as JSON. The response format is as follows:
publisher: "Roli Books Pvt Ltd",
author: "Kuldip Nayar"
This response contains the URL of the book, source, MRP of the book, price at which the book is being sold, publisher, and author. The Scan and Search feature of LowPrice uses this API to get the prices of books. How does the App get the ISBN number? That will be explored in Part 2.
2. Search by Title/Author/Publisher
Not always does everybody have the ISBN number of a book that they want. Most of the times, we know the title/author/publisher of a certain book. To enable free text search, the API also provides a method to search by these attributes of a book as well. LowPrice uses this API to power book search. If we would like to search by the name of a book eg: Harry Potter,
http://api.dataweave.in/v1/book_search/searchByTitle/?title=harry%20potter . The response from the API is as follows:
title: "HARRY POTTER AND THE CHAMBER OF SECRETS",
author: " J. K. Rowling",
mrp: " 399",
Now that we have the data in an easy to consume format, let us now focus on building the Android app itself.
How to do a REST call in Android
All of DataWeave’s API are REST based. Let us see how we can consume a REST API in Android.
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://api.dataweave.in/v1/book_search/searchByTitle/?title="+bookname);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
With this simple 4 lines of Code we are able to consume the data in our app. Since the API returns the data as JSON, lets see how to parse this data.
How to parse JSON in Android
Android SDK provides us with an option to natively parse JSON. Optionally we could also use Jackson or GSON.
JSONArray json = null;
List mElementList = new ArrayList();
json = new JSONArray(jsonString);
This simple code will help you to parse JSON response which we receive from our REST API.
In this post we took a brief look at DataWeave’s API which is powering LowPrice backend and we also saw how to consume this API in android and use the data in a way we want. In the next post, I will explain how I built the Scan and Search feature in LowPrice.
Building data apps has never been easier!
Feel free to download and check out lowprice android app from here: https://play.google.com/store/apps/details?id=com.binarybricks.lowprice
Please share your feedback.