MP2: Client-Server Communication

Created By: Geoffrey Challen
/ Updated: 2022-05-23

Let's continue our work on the machine project! Next we'll examine one of the more powerful aspects of our app: the communication between the API server and app client.

HTTP Clients and Servers

Your Android app is unusual in that in includes a small webserver that provides an application programmer interface (API) over HTTP, the hypertext transfer protocol. Normally the server would run on another machine, and would communicate with your app over the internet. But in our case, it runs alongside the rest of your app.

HTTP is the one of the main protocols used by devices on the internet to exchange information. Among other things, it provides a way for one device (the client) to request data from another device (the server). HTTP is used by the world wide web, and every time you browse the internet, your web browser is making a series of HTTP requests to various servers located nearby or all over the world.

Internet communication is a very big and fascinating topic, and certainly not something that we can do justice in a single lesson! But we'll point out some of the basics of the HTTP protocol as we examine the server and client code we've provided in your app.

Restaurant API Server

There are two test cases we'll examine in this lesson. The first tests your API server, the code that provides data to your client. This would normally run on a server in the cloud somewhere. Let's examine that code together and discuss the next steps you need to complete to continue the project.

Walk through existing API server code and discuss how the /restaurants route is established.

Networking Callbacks

We're about to examine code in our API client that uses a callback method. We've seen a callback used previously in our MainActivity to allow us to react when the user interacts with the interface. But why are callbacks used as part of a network request? Let's discuss!

Discuss why network requests use callbacks—to avoid blocking waiting for the request to complete. This explanation should be language agnostic.

Restaurant API Client

Do not proceed until testPreferencesRoute is passing. You will not be able to successfully test your API client until the server component is completed.

The next test case tests your API client, the code that retrieves the data from the server and provides it to your app. This would normally run on a client device like a phone, to retrieve the latest information about local restaurants from the API server.

Walk through the existing API client code and discuss how it works. Point out that it uses a callback, and draw on the discussion in the previous video.

How to Learn More

The Internet is probably the most important piece of technology that computer scientists have ever created. Without the ability to communicate across the globe, computers would really be nothing more than glorified typewriters or calculators.

There are a lot of ways to find out more about the internet. But I'll take the liberty of pointing you at some videos that I've created on the topic that you might enjoy. Note that this is completely optional content! The playlist above focuses on internet concepts related to the world wide web, including the HTTP protocol. But there is a much longer playlist here that covers everything from physical infrastructure all the way up to Internet policy.

No Homework for This Lesson

As a reminder, on lessons where we focus on the machine project we will not assign a homework problem! However, the lesson will usually focus on helping you complete a particular part of the MP test suite, and so we encourage you to spend time on that in lieu of a homework problem.

Right now your goal should be to

  1. Add the /preferences route to your API server and pass the 10-point test case
  2. Complete the getPreferences method on the app client and pass that 10-point test case

If you get stuck, find us for help on the help site or forum.

MP2 Scores

More Practice

Need more practice? Head over to the practice page.