Android Tutorial for Beginners: Part 3

An Android Tutorial that shows you how to make your first app app step-by-step, using Android Studio! By Darryl Bayliss.

Leave a rating/review
Save for later
Share
You are currently viewing page 2 of 6 of this article. Click here to view the first page.

A Glance at Gradle

When you created your project in Android Studio, you may remember mentions of Gradle and Maven. Refer to Part One if you need a reintroduction. Now you’ll see them in action.

Open build.gradle. Note that there are two build.gradle files in your project. You want the one that’s within the app folder – not the one at the project root level:

build_gradle_file

Some of the stuff happening in here is beyond the scope of this tutorial, but if you become serious about Android development, I recommend looking into Gradle a little more, starting of course, with the Gradle website. For now, just notice that the Android plugin is being applied to the project (apply plugin: 'com.android.application').

Scroll down to the area labeled dependencies. There may already be a support library listed there, or it may be empty. You’re going to add two new libraries.

Add the libraries like this:

dependencies {

	...

	// there may or may not be a support library above these
	compile 'com.loopj.android:android-async-http:1.4.4'
	compile 'com.squareup.picasso:picasso:2.1.1'
}

Then find the Sync Project with Gradle Files button on the Studio toolbar and press it. It looks like this:

sync_gradle

Note: If you get an error that says “requires compiling with JDK 7”, you should upgrade your SDK to JDK 7. You can download it here, then follow the prompts to set your SDK folder to /Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home.

Believe it or not, that’s it! Just like that, you’ve included the Android Async Http and Picasso libraries in your project and you can start using them whenever you like.

It’s so easy because both of these libraries are available via the Maven Central Repository, to which your project already contains a reference. You can see this in your root folder’s build.gradle file, it looks like this.

repositories {
        jcenter()
    }

So, when you tell Gradle which libraries you’d like to use, it simply grabs them from the source and you’re good to go.

jcenter however is thought of as being faster and more responsive whilst still providing all the libraries available on the Maven Central Repository. So lets leave this alone.

Note: jcenter() is a Gradle method that connects to a repository which is a superset of the Maven Central Repository. In early versions of Android Studio the default repository set by Gradle was Maven Central, you still change it back by changing jcenter() to mavenCentral().

If you need to include any libraries which are not available on the Maven Central Repository, then you’d still have to go through the old school method of copying the source (or the library) into your project. But most of the time, you won’t have to go through all that pain since the Maven Repository contains a lot of third-party libraries for you to use. So you’ll probably be able to find an alternative to the library you’re interested in. If interested, you can even browse all the libraries available on the Maven Repository.

JSON Basics

Great! Now it’s time to meet your datasource: the Open Library API. It’s a constantly-updated database of books, searchable by author and title. The wealth of data is enormous!

Try this query as an example: http://openlibrary.org/search.json?q=hunger+games+suzanne+collins

This is a relatively simple URL to understand; whatever is typed in after the ?q= is the query string that will be used to search the database. Feel free to change it to a different author name/title and compare results, remembering to use + to separate words.

The result is in the form of a large JSON response. Take a minute to look around at the sort of data the response includes.

If you’re not familiar with the JSON format, I suggest a quick glance through the JSON page. The basics are that there are two ways data can be arranged in JSON: arrays and objects.

JSONArrays list objects of the same type. For example, a list of books might look like this:

["The Hunger Games", "Harry Potter and the Sorcerer's Stone", "A Game Of Thrones"]

JSONObjects are a collection of key-value pairs. For example, a very simple object describing a book might be:

{"title" : "The Hunger Games", "author_name" : "Suzanne Collins"}

The results from your queries to the Open Library API are really just expansions on those two basic structures. They are larger, and nested into several levels, but the ideas remain the same.

Creating a Query

Now that you know roughly what to expect from the datasource, it’s time to set up the code to go make a sample query!

Add the following variable to your growing list at the top of MainActivity.java:

private static final String QUERY_URL = "http://openlibrary.org/search.json?q=";

The above is simply a reference to the URL you’ll be calling. It’s much better to hold this URL as a static String, so you don’t have to go searching through your code for it. Remember that you’re going to append the search string after the ?q=.

Next, add this new method anywhere in your MainActivity.java Class:

private void queryBooks(String searchString) {

    // Prepare your search string to be put in a URL
    // It might have reserved characters or something
    String urlString = "";
    try {
        urlString = URLEncoder.encode(searchString, "UTF-8");
    } catch (UnsupportedEncodingException e) {

        // if this fails for some reason, let the user know why
        e.printStackTrace();
        Toast.makeText(this, "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
    }

    // Create a client to perform networking
    AsyncHttpClient client = new AsyncHttpClient();

    // Have the client get a JSONArray of data
    // and define how to respond
    client.get(QUERY_URL + urlString,
            new JsonHttpResponseHandler() {

                @Override
                public void onSuccess(JSONObject jsonObject) {}

                @Override
                public void onFailure(int statusCode, Throwable throwable, JSONObject error) {}
            });
}

queryBooks handles the network call to the API. It encodes the input searchString into URL format, then appends that to the base URL you specified at the top of the class.

Calling new AsyncHttpClient() simply creates an instance of the HTTP client. It’s got a lot of great methods built-in, but the only one you need here is get(String url, ResponseHandlerInterface responseHandler).

The get method takes in two parameters:

  • String url is simply the URL from which you’d like to fetch data. This parameter will be made up of base URL defined at the top of your class, plus the search string you will add later.
  • JsonHttpResponseHandler, which you define now, even though you don’t know whether the network call will succeed or fail, or how long it will take either way. It contains methods called onSuccess and onFailure to respond to the two cases once the handler does get a response from the server.

You might have noticed that onSuccess and onFailure are currently method stubs with no code. Fix that by fleshing out the onSuccess to match the following:

@Override
public void onSuccess(JSONObject jsonObject) {
    // Display a "Toast" message
    // to announce your success
    Toast.makeText(getApplicationContext(), "Success!", Toast.LENGTH_LONG).show();

    // 8. For now, just log results
    Log.d("omg android", jsonObject.toString());
}

Next, implement onFailure as follows:

@Override
public void onFailure(int statusCode, Throwable throwable, JSONObject error) {
    // Display a "Toast" message 
    // to announce the failure
    Toast.makeText(getApplicationContext(), "Error: " + statusCode + " " + throwable.getMessage(), Toast.LENGTH_LONG).show();

    // Log error message
    // to help solve any problems
    Log.e("omg android", statusCode + " " + throwable.getMessage());
}

In both cases, you simply present a Toast and log the results. Soon, though, the success case will get a lot more exciting.