In my Android application I have one EditText
, one Button
, and one Listview
. When I type a movie name into my EditText
field and press on the Button
, I want the ListView
to be populated with movie names from the Rotten Tomatoes website that match what I entered into the EditText
field.
But I can't figure out how to use the Rotten Tomatoes JSON API to get the movie data. How do I do it?
A typical approach to this kind of problem would be:
Create an
AsyncTask
that handles the networking and parsing of your request and the response, since long running operations in the main (or UI) thread is a bad idea. In the AsyncTask you communicate to the API server using a HttpClient and parse the JSON request/response using a JSON parser library, such as Google's gson.You can find plenty of tutorials on how to communicate with remote servers using the HttpClient, here is one of them (I can't vouch for it's quality):
http://www.mysamplecode.com/2011/09/android-asynctask-httpclient-with.html
Basically, you need to do four things:
http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey=[your_api_key]&q=[search_keyword]&page_limit=[page_limit]
, as shown on this page.I've put together a small demo app that will do this. Please try out the code below.
MainActivity.java
res/layouts/activity_main.xml
And add this line to your
AndroidManifest.xml
(it gives your Android app permission to use the Internet, which you obviously need to make the request to Rotten Tomatoes' web server):Bonus answer:
If you want "live" search results as you type the search keyword into the EditText field, add a TextWatcher via EditText's
addTextChangedListener()
method, and make it do the HTTP request inonTextChanged()
.