In this codelab, you'll make an app that allows you to look up the current lowest price of a book on Amazon.com.
An application programming interface (API) is the messenger that takes your requests, tells a system what you want to do, and returns the response back to you.
If you have ever used the Facebook app on your phone, you are using a client program that communicates with the Facebook API server program.
In this codelab, you will create an Android client app that communicates with the Amazon API. Your app will request book and ISBN information from the Amazon API, and the API will return the list price of the book. Your app will then present the book data to the user.
Just like people from different cultures have different customs (e.g. shaking hands when meeting or kissing each other on the cheek), different APIs have different communication protocols. In this step, you will learn how information is formatted when communicating with the Amazon API.
Get value
.
The web page will display the top five books returned from Amazon.Can you explain the different parts of the output shown below?
[['"Dance: Cinderella Retold (Romance a Medieval Fairytale series Book 3)"', '0.0', 'B01NCZ6T85'],
['"Dance"', '$10.43', '1481487078'],
['"Dance with Deception: Scandalous Secrets, Book 1 (Scandalous Secrets - Exclusive Edition)"', '0.0', 'B00NO2V0CS'],
['"A Dance with Dragons (A Song of Ice and Fire)"', '$5.33', '0553582011'],
['"Dance Mania Mad Libs"', '$2.09', '0843137126']]
Which book has ISBN 0062363603?
Create a new project called
AmazonBooks
(no spaces).
Design your user interface so that at a minimum it has the following elements:
http://aiamazonapi2.appspot.com/
.When the user enters a search term and clicks on the Keyword Search Button, we want to invoke an Amazon search.
We can access the Amazon API by using our TinyWebDB component, which works just like the TinyDB component we used in a previous codelab.
Make the correct Amazon search when the user clicks on the Keyword Search Button. The following blocks may be useful for this part:
Button.Click
Event Handler blockTinyWebDB.GetValue
blockTextBox.Text
blockMake the ResultsLabel display the value returned by the Amazon search.
Make sure to check if the returned value is a list before displaying it. It won't be a list if the Amazon API is offline, or if no results can be found.
The following blocks may be useful for this part:
TinyWebDB.GotValue
Event Handler blockif/then
conditional blockis a list?
blockset Label.Text to
blockget valueFromWebDB
blockThe code for searching by ISBN is similar to searching by keyword, but
in this case, the Amazon API expects the tag to be in the form of
isbn:xxxxxxxxxxxxx
. Your user should not need to know this
protocol, so we want our app to add the "isbn:" prefix to the user's input.
Make the correct Amazon search when the user clicks on the Search by ISBN button. The following blocks may be useful for this part:
Button.Click
Event Handler blockTinyWebDB.GetValue
blockTextBox.Text
blockjoin
block""
(string) blockWhen you call a web service (API) with TinyWebDB.GetValue
,
there may be a delay before the data arrives and TinyWebDB.GotValue
is triggered. It is generally good practice to notify users that the
request is being processed to reassure them that the app has not
simply frozen.
When the user clicks on either the Keyword Search Button or the ISBN Search Button, make the Results Label display "Searching Amazon...". The following blocks may be useful:
set Label.Text to
block""
(string) blockTyping on a cell phone can be difficult and time-consuming for some people, so it would be nice if users could scan the barcode of a book instead of typing in the ISBN. Fortunately, App Inventor's Barcode Scanner component makes this feature easy to implement.
Start up the scanner when the Scan Button is clicked.
Start an ISBN search once the barcode has been scanned. The following blocks may be useful:
when BarcodeScanner.AfterScan
Event Handler blockset Label.Text to
blockset TextBox.Text to
blockTextBox.Text
blockjoin
block""
(string) blockget result
call TinyWebDB.GetValue
Right now, the Results Label simply displays the value returned by
the Amazon API. While this does provide the user with all the
information, it is a bit difficult for users to read. In this step, your
goal is to modify the when TinyWebDB.GotValue
event handler
so that the app displays book information in an easy-to-read, organized
manner.
resultList
to be
an empty list. Hint: Use the create an empty list
block.""
).
Name the variables title
, cost
, and isbn
.
resultList
variable.""
).for each...in list
block to loop through each
book result in resultList
. For each book result, assign
the correct values to the title
, cost
, and
isbn
variables. Hint: Use the select list item
block to select specific items from a list.Your code should look something like this.
You have used App Inventor to create the Amazon Books app.