Lesson Weekend

Almost every Android app interacts with a web server to retrieve information, store data, and interact with users. In this section we'll explore, install, and configure a tool called OkHttp, which will allow us to send and receive requests to the Yelp API.

Requesting Information from an API

You've probably worked with APIs in your previous courses here at Epicodus. APIs are a web service. We can access their information by submitting a GET request to a specific URL of the API, including any parameters that specify the type or amount of information we're requesting.

After receiving a request the API's server sends a response containing a status, headers, and a body. If everything is executed successfully the body will contain the data we have requested.

Contacting APIs in Android

However, in mobile development contacting an API can be a bit trickier. For instance, devices often switch back and forth between using a WiFi and cellular connections. And sometimes these connections aren't that great. This can make connecting to APIs difficult. Thankfully, there are several tools designed to gracefully handle the inconsistency of mobile connections while simultaneously making it easier to construct and send requests.

OkHttp

OkHttp is one of several HTTP networking libraries available for Android. It manages the process of creating, sending, and receiving HTTP requests and responses. Additionally, it offers several features that make it particularly beneficial to mobile applications:

  • Automatically alternates IP addresses (if possible) if a connection to a web service fails for any reason.
  • Pools connections to reduce latency. (Connection pooling is the act of recycling previous connections for future requests.)
  • Caches responses to avoid sending duplicate requests.
  • Builds and sends requests.

Essentially, if any issues occur in contacting the API, OkHttp will attempt to resolve these issues for us before giving up, sending errors, or crashing our application entirely.

Adding OkHttp to MyRestaurants

We've already added our credentials to our application. Let's also add OkHttp in anticipation of making HTTP requests in the following lessons. First, we'll add OkHttp as a dependency in our build.gradle file:

build.gradle(Module: app)
...
   dependencies {
        ...
       compile 'com.squareup.okhttp3:okhttp:3.2.0'
       ...
  }

Requesting Internet Permissions

In order to contact an API web service, our application will need internet access. In Android applications internet access is not provided by default. Any application that requires internet access must explicitly request access in its manifest.

To do this, we can add the following line to our manifest:

app/manifests/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.epicodus.myrestaurants">

    <uses-permission android:name="android.permission.INTERNET"/>
...

The <uses-permission> element requests access to the specified resource from the user.

The OkHttp library should now be included in our MyRestaurants application. We're almost ready to make API requests! In the next lesson we'll learn about API security, what those credentials we placed in our Constants class are meant for, and how to use them to successfully gather information from an outside web service.


Example GitHub Repo for MyRestaurants

Terminology


  • OkHttp: An Android Networking library developed by Square. It handles making HTTP requests and receiving responses for us, with built-in features to compensate for poor network connections, and minimize data usage.

Examples


Requesting internet access permissions in the manifest:

app/manifests/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.epicodus.myrestaurants">

    <uses-permission android:name="android.permission.INTERNET"/>
...

Example GitHub Repo for MyRestaurants

Additional Resources