TheMovieDb API – Part 1

TheMovieDb API android app screenshots

It’s API time and TheMovieDb api is one of the best for learning and building an android application that connects to a REST service, saves offline through a Content Provider and can be unit tested thoroughly.

We will do it by parts so everyone can enjoy the full ride. I will assume you have already install Android Studio 2.0 from the official website. The code will be on this GitHub repo so you can view/download/doItYourself on the go.

First we will do a super refresh and create our android project, go ahead and launch Android Studio and click “Start a new Android Studio Project”

Android Studio
Android Studio

Good, now time to configure your project, go ahead and set any application name you want under the respective field as well as a Company Domain (which is your reverse domain name) and a project location, if you have problems refer to the screenshot so you can see the light.

Android application configuration screen
Android application configuration screen

So far so good, now for our platform target, in our case we are going to support phones and tablets all the way back to android Jelly Bean which will run on 94.8% of the devices, neat ha?

Android form factors setting
Android form factors setting

Nice job, now the beauty of Android Studio really steps up in this next setting which is the ability to preset a pre-defined template into our project. In the “Add an Activity to Mobile” select “Empty Activity” and click next. You will be prompted to select an activity name and a layout name leave them both with the default names (MainActivity, activity_main) and click Finish.

Android Studio Templates
Android Studio Templates
Create a new empty activity
Create a new empty activity

Awesome! You are almost set to go with your android application that will connect to TheMovieDb.org API, if you have something similar like the screenshot below you will then only need one last thing before moving on.

Android application basic structure
Android application basic structure

Now, as you know we are going to need an API key which you are suposse to get from TheMovieDb.org so we can connect to their API, once you get your key (you can look it up thouth your profile at their website) lets make a quick test in android to see everything is alright and at the same time quickly introduced you to test driven development.

First let’s make sure that under our app/build.gradle file we have junit,a unit test framework, under dependencies.

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
}

Now that we took care of that let’s secure our API key that we obtained from TheMovieDb.org. Todo so we will create a variable under ourApp/gradle.properties

You should place it similar to:

themoviedbapikey = theapithatigotfromthemoviedb

Awesome, now you are protecting your API key by separating it from your java files and to get access to it we will now create what is called a BuildConfigField. Go to your app/build.gradle file once more and under buildTypes set the following:

debug {
 buildConfigField "String", "THEMOVIEDB_KEY", "\"$themoviedbapikey\""
}

Excellent, now let’s make our first unit test! We are going to use one of the example endpoints from TheMovieDb.org to see if we get a HTTP_OK response (200). Under app/src/test/ you will find an already made unit test class with a simple assert test in it you should change it for the following:

private static final String BASE_URI = "https://api.themoviedb.org/3/movie/550?api_key=";

@Test
public void testTheMovieDbEndPointService() {
    HttpURLConnection conn = null;
    try {
        URL url = new URL(BASE_URI + BuildConfig.THEMOVIEDB_KEY);
        conn = (HttpURLConnection)url.openConnection();
        Assert.assertTrue(conn.getResponseCode() == HttpURLConnection.HTTP_OK);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Hit run and if it’s green and the test pass CONGRATULATIONS!

Unit test success
Unit test success

Now you have quickly refreshed how to create a project on android studio, but be warned the next steps will go right on point so we cover what we really need. Don’t forget we also did our first unit test, to check if we had a HTTP_OK back from TheMovieDb using an endpoint provided by them along with our assigned API key.

On bonus side the following are the dependencies we will be using in our android application, Hey I know what you thinking “Here we go with the crazy list” we’ll not quite like Javascript framework, no pun intended. If you can add them now under app/build.gradle. Most of the dependencies shown are the typically you will find to bootstrap an android app, they have being test thoroughly and actively receiving the community support.

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile "org.mockito:mockito-core:1.+"
    testCompile 'junit:junit:4.12'

    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:cardview-v7:23.4.0'
    compile 'com.android.support:design:23.4.0'
    compile 'com.android.support:recyclerview-v7:23.4.0'

    //easy REST interfaces
    compile 'com.squareup.retrofit2:retrofit:2.0.2'
    //easy image caching
    compile 'com.squareup.picasso:picasso:2.5.2'
    //easy POJO's creation
    compile 'com.squareup.retrofit2:converter-gson:2.0.0'

See you on part 2, happy coding

Joel

Please follow and like us:
Facebook
Twitter
Joel Sosa

Author: Joel Sosa

Android nanodegree holder | Graduate Student @GeorgiaTech CS Interactive Intelligence | @gdgpuertorico Organizer