-
Notifications
You must be signed in to change notification settings - Fork 5
Amazon Books
In this lesson we will be using a barcode scanner to lookup book prices on an Amazon.com API (Application Programmer Interface) Note: You will need to install this android barcode scanner app before starting this project. (https://play.google.com/store/apps/details?id=com.google.zxing.client.android&hl=en)
Add these block components to your code to use the phone's barcode scanner. Note that you will need a barcode scanner installed for this to work.
A TinyWebDB is a component used to send and receive data from the internet. In this case, we will be interacting with the Amazon.com book API TinyWebDB. An API is an Application Programmer Interface that allows software developers to use information and computations from other developers' software, over the internet. We will be using the Amazon.com book API to get title and cost information.
Here is an example of the purple block needed to use a TinyWebDB. In this example, we request information from the TinyWebDB from a string, the text of SearchTextBox in this case.
You will also need to configure your TinyWebDB to use the Amazon.com API (Application Programmer Interface). Paste this url into the ServiceURL field http://aiamazonapi2.appspot.com/
We will need a text box, three buttons, labels, a TinyWebDB, and a BarcodeScanner.
- Create event handlers for all 3 button click events.
- For the KeywordSearchButton, use the text in the search box to call the TinyWebDB GetValue block.
- The internet will take some time to get back to you when you request the book information from Amazon. When the TinyWebDB GotValue happens, check to make sure the valueFromWebDB variable is actually a list.
- If the valueFromWebDB is a list, set the result label to show the valueFromWebDB.
You should see results when searching for a keyword like "ice cream". Don't worry if the results look strange. The results are actually a list, of lists. The overall list is all the books matching the keyword. The inner lists are the title, cost, and ISBN numbers of the specific book
- For the ISBNSearchButton, use the text in the search box, combined with "isbn:", to call the TinyWebDB GetValue block. The format should look something like this "isbn:9783161484100". (hint: use a join)
- Now modify your code for ISBNSearchButton and KeywordSearchButton to set the result text to "Searching..." right when the button is clicked, before making the database call. That way, your users know that the button was clicked, and they should wait.
- When clicked, call the DoScan function of the BarcodeScanner.
- After the BarcodeScanner scans, set the ResultsLabel to something appropriate, to let your user know that the barcode was scanned and the search is in progress.
- Also, set the SearchTextBox with the "isbn:XXXXX...." so that the user can see the search query being performed.
- Next, call the TinyWebDB GetValue function with the "isbn:XXXXX...." to query the Amazon.com API
Make sure that the scan button works, and that a result is displayed. We will make the results look better in the next step.
- Use a variable to store the result of the TinyWebDB GotValue. Be sure to initialize this variable to be an empty list.
- Use variables for title, cost, and isbn to identify the corresponding list items for each book. For example, list item 1 is the title. Set these variables for each book by looping through all the books. Be sure to initialize these variables to be empty strings.
- Use these variables to display the results in a readable form. Your goal is something like this. (hint: use a join to combine results from previous iterations of the loop)
title: Harry Potter
isbn: 0954838354
price: $42
title: Harry Potter, Complete Edition
isbn: 095483334
price: $456
- Add another button that will allow you to speak search keywords. After you speak the keywords, the app should put the spoken text into the search bar, and search just like if you were to press the Search by Keyword button.
- It is silly that you have to erase the sample text "Enter ISBN or Keywords" when you want to search for an item. Erase the text when the search box gets focused (a user clicked on it).
- When a search is complete, have the app speak, and tell you what the best price is. You will have to write code to figure out the lowest price is, then use text-to-speech.