HTTP Basics - Networking with Android, Android Studio, and Kotlin

preview_player
Показать описание
Learn about basic concepts of HTTP requests, JSON, using REST APIs and consuming responses, and use the Postman REST client to investigate the GitHub API.

Watch the full course over here:

---

We are also focused on developing a strong community. Our goal is to help each other reach our dreams through friendship and cooperation. As you can see below, a bunch of us have joined forces to make this happen: authors, editors, subject matter experts, app reviewers, and most importantly our amazing readers!

--

In addition to providing standard network connectivity, Android provides APIs to let your app connect and interact with other devices with protocols such as Bluetooth, NFC, Wi-Fi P2P, USB, and SIP.

Perform network operations overview

This class explains the basic tasks involved in connecting to the network, monitoring the network connection (including connection changes), and giving users control over an app's network usage. It also describes how to parse and consume XML data.

By going through these lessons, you'll have the fundamental building blocks for creating Android applications that download content and parse data efficiently, while minimizing network traffic.

Connect to the network

Design secure network communication
Before you add networking functionality to your app, you need to ensure that data and information within your app stays safe when transmitting it over a network. To do so, follow these networking security best practices:

Minimize the amount of sensitive or personal user data that you transmit over the network.

Send all network traffic from your app over SSL.

Consider creating a network security configuration, which allows your app to trust custom CAs or restrict the set of system CAs that it trusts for secure communication.

For more information on applying secure networking principles, see Android's networking security tips.

Choose an HTTP client

Most network-connected Android apps use HTTP to send and receive data. The Android platform includes the HttpsURLConnection client, which supports TLS, streaming uploads and downloads, configurable timeouts, IPv6, and connection pooling.

Introduce network operations on a separate thread

To avoid creating an unresponsive UI, don't perform network operations on the UI thread. By default, Android 3.0 (API level 11) and higher requires you to perform network operations on a thread other than the main UI thread; if you don't, a NetworkOnMainThreadException is thrown.

The following Activity snippet uses a headless Fragment to encapsulate asynchronous network operations. Later, you will see how the Fragment implementation, NetworkFragment, accomplishes this. Your Activity should also implement the DownloadCallback interface, allowing the fragment to call back to the Activity in case it needs connectivity status or needs to send an update back to the UI.

Implement a headless fragment to encapsulate network operations

Since the NetworkFragment runs on the UI thread by default, it uses an AsyncTask to run the network operations on a background thread. This Fragment is considered headless because it doesn't reference any UI elements. Instead, it is only used to encapsulate logic and handle lifecycle events, leaving the host Activity to update the UI.

When using a subclass of AsyncTask to run network operations, you must be cautious that you don't create a memory leak in the case where the Activity that is referenced by the AsyncTask is destroyed before the AsyncTask finishes its background work. To ensure this doesn't happen, the following snippet clears any references to the Activity in the Fragment's onDetach() method.

Use HttpsUrlConnection to fetch data

In the snippet above, the doInBackground() method runs in a background thread and calls the helper method downloadUrl(). The downloadUrl() method should take the given URL and use it to perform an HTTP GET request. Once a connection has been established, you should use the method getInputStream() to retrieve the data as an InputStream.

Convert InputStream to a string

An InputStream is a readable source of bytes. Once you get an InputStream, it's common to decode or convert it into a target data type.

Survive configuration changes

So far, you have successfully implemented an Activity that performs a network operation. But, if the user decides to change the device configuration (i.e. rotate the screen 90 degrees) while doInBackground() is running on the background thread, the Activity destroys and recreates itself, causing it to re-run onCreate() and reference a new NetworkFragment (see Runtime Changes guide).
Рекомендации по теме