Chapter 9. Networking

While it’s certainly possible to have a largely self-contained app—perhaps it only makes HTTP requests to update its version using built-in, automagic Play update infrastructure—it’s rare in our connected world. Whether you need to authenticate users, post usage metrics to your analytics account, read content or media, download or upload files, listen for push notifications, or just sync up with a time server, you’ll want to know how to make an HTTP request and understand an HTTP response.

Fortunately, the HTTP spec has a very simple rules set, and most people find it easy to understand and learn. At the risk of being too simplistic, imagine an HTTP transaction consists of two parts: a request and a response. Both are represented as simple (and often human-readable) text files. The first line of each describes some basic attributes about the file (like the URL for a request, and the status for a response). The next lines are header information in traditional key-value-pair syntax: header-name: Header Value. Then a “blank” (empty) line, and you’re at the body. That’s it! For more info, check out “HTTP Made Really Easy”.

From high-level authentication protocols to streaming encrypted video data to uploading every image on your phone in the background, all of these operations use the same basic tenets described previously.

Tasks

In this chapter, you’ll learn to:

  1. Read and print a text file from a remote server.

  2. Make an HTTP POST request.

  3. Download a binary ...

Get Native Mobile Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.