Exercise Tuesday

Goal: Practice making requests to a backend web service using OkHttp, Signpost (if needed), and displaying information in a custom list item layout using a RecyclerView.

Warm Up


  • What is the 'main/ui thread' and why shouldn't we run long tasks such as a request on this thread?
  • Why is a RecyclerView preferable to a ListView?
  • What are the necessary components of an OkHttp request to an API? What series of events occur in a successful request?
  • What is OAuth protocol and why would we use it?

Code


MyRestaurants

Before beginning the weather project, follow along with this week's homework thus far and integrate these new concepts into your own MyRestaurants application. This includes adding OkHttp to retrieve information from the Yelp API and parsing the JSON response data.

Then, after confirming your app can successfully retrieve and log data from the API, add the RecyclerView and adapter to display customized restaurant list items.

Weather API

Build a weather app using the Open Weather Map API. Allow users to enter a location and receive current weather data for that region. Include a new activity with a RecyclerView that displays the 7-day forecast for the user entered location using a custom list item layout.

Unlike the MyRestaurants example, Open Weather Map doesn't require authentication to make a request, here's how this might differ from MyRestaurants:

OkHttpClient client = new OkHttpClient.Builder()
        .build();

HttpUrl.Builder urlBuilder = HttpUrl.parse(Constants.API_BASE_URL).newBuilder();
urlBuilder.addQueryParameter(Constants.YOUR_QUERY_PARAMETER, <your string parameter here>);
urlBuilder.addQueryParameter(Constants.API_KEY_QUERY_PARAMETER, Constants.API_KEY);
String url = urlBuilder.build().toString();
Constants.java
public static final String API_KEY = BuildConfig.API_KEY;
public static final String API_BASE_URL = "www.<api url>.com";
public static final String YOUR_QUERY_PARAMETER = "<your query parameter string>"; //Example: "location"
public static final String API_KEY_QUERY_PARAMETER = "appid"; 

Peer Code Review


  • Can the application request data from an API?
  • Does the application parse this response data appropriately?
  • Is the code well-refactored and easy to follow?