Connecting to the internet

There are multiple ways an Android app can communicate over the internet. Two of the most common ways are through HTTP or HTTPS, and sockets. Sockets maintain a persistent connection once established. That makes them suitable for real-time apps like chat. Unlike sockets, using HTTP or HTTPS requires users to make a new request for every server response. In this course, you’ll focus only on HTTP and HTTPS.

Knowing The Difference Between HTTP and HTTPS

HTTP and HTTPS are two of the most common protocols used in web communication.

  • HTTP: Hypertext Transfer Protocol
  • HTTPS: Hypertext Transfer Protocol Secure

HTTP transmits data in plain text, while HTTPS encrypts data before transmission. Using HTTP means anyone overlooking communication between your app and a server can see your data, including sensitive data like passwords or credit card numbers. Because of this, HTTPS is the recommended standard for all web communication, and you’ll use it in the sample app. The following diagram visualizes the difference between HTTP and HTTPS.

User Insecure Connection Normal HTTP HTTP vs HTTPS User Encrypted Connection Secure HTTPS SSL Certificate
HTTP vs HTTPS

Sometimes, it’s easier to test your code in a controlled development environment using only HTTP. If you want to do that, you must explicitly let the system know by adding the following code in AndroidManifest.xml: android:usesCleartextTraffic="true". Starting with Android 9 (API level 28), cleartext support is disabled by default. On versions below, this flag is set to false.

Understanding REST

REST stands for REpresentational State Transfer. It’s an architectural style for designing web APIs based on six rules:

  • Uniform interface
  • Client-server
  • Stateless
  • Cacheable
  • Layered system
  • Code on demand (optional)

You won’t dive deeply into REST because it’s more important for API developers than for API consumers. It’s just important to differentiate between REST and HTTP.

HTTP Methods

HTTP defines a number of methods for interacting with a server. In this course, you’ll use the four most commonly used methods:

  • GET: Used to retrieve data from a resource. Think of it as “asking” for information.
  • POST: Used to create a new resource on the server. Think of it as “sending” new information.
  • PUT: Used to update an existing resource on the server. Think of it as “replacing” existing information.
  • DELETE: Used to remove a resource from the server. Think of it as “throwing away” information.

Demystifying URLs

Every web address you see, such as https://www.example.com/path/to/resource, is actually a URL (Uniform Resource Locator) broken down into parts:

  • Scheme: The protocol used, such as https or http.
  • Host: The server’s domain name, such as www.example.com.
  • Path: The resource’s specific location on the server, such as /path/to/resource.
  • Query string: Optional parameters appended to the path, starting with ?, such as ?key=value.
  • Fragment: An optional identifier within the resource, starting with #, such as #section.

Understanding these parts is crucial for constructing and interpreting URLs correctly.

Introducing JSON

JSON (JavaScript Object Notation) is a lightweight, human-readable data format widely used for exchanging information between web apps. It represents data as a collection of key-value pairs, nested objects, and arrays, making it flexible and easy to parse.

Parsing JSON

When you get a JSON response from the server, it’s just a string. Parsing is the process of transforming the string into another format. In this case, you’ll parse JSON into Kotlin objects.

Types in JSON can be primitive or composite. Primitive types include:

  • String: A sequence of characters enclosed in double quotes, supporting Unicode characters. An example of this is "Hello, world!"
  • Number: This can be an integer or a decimal number, such as 123 or 3.14159, for example.
  • Boolean: This represents true or false values.
  • Null: This represents the absence of a value.

Composite types can be:

  • Array: An ordered collection of values, enclosed in square brackets []. Values can be of any type, even mixed within an array. Here’s an example: [1, "two", true] -> Arrays get parsed into Kotlin collections.
  • Object: An unordered collection of key-value pairs, enclosed in curly braces {}. Keys must be strings, and values can be any type. An example of this is: {"name": "John", "age": 30} -> Objects get parsed into Kotlin data classes.

In Android, you have access to the JSONObject class. It’ll help you read and write JSON for now. In later lessons, you’ll learn about a popular JSON parsing library called Moshi.

Making Your First Network Request

Now that you have the basics, it’s time to introduce the HttpsUrlConnection class. This is a built-in Java class that lets you make network requests in Android. It provides methods for:

  • Setting the URL and HTTP method (GET, POST, etc.).
  • Adding headers to the request.
  • Sending data with the request (for POST and other methods).
  • Receiving a response from the server.
  • Reading the response data.

Although it offers flexibility, HttpsUrlConnection can be verbose and require manual handling of details, as you’ll see in the next section.

In the next section, you’ll learn how to make an HTTP request with HttpsUrlConnection.

See forum comments
Download course materials from Github
Previous: Demo 1 Next: Demo 2