Android Networking: Fundamentals

Sep 6 2022 · Kotlin 1.6, Android 12, Android Studio Chipmunk | 2021.2.1 Patch 1

Part 1: Learn About HTTP & Threading

03. Connect to the Internet

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 02. Set Up the Project Next episode: 04. Create a HTTP Connection

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Notes: 03. Connect to the Internet

The student materials have been reviewed and are updated as of July 2022.

Transcript: 03. Connect to the Internet

Fetching data from a server requires an Internet connection.

Before connecting to the Internet, Android requires of you to set up permsissions in the Android Manifest.

It is also a good practice to check whether or not a mobile phone is connected to a network, before reuqesting any data, as you’re only wasting resources starting connections, in case there isn’t a network available. Let’s see how to achieve this!

To start, open the AndroidManifest.xml and add the following two permissions to it.

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

The first permission is just in general, to access the Internet, and the second is to check the state of the connected network, to see if it’s a Wi-Fi connection, or a mobile connection.

Now, create a new class in the networking package, and name it NetworkStatusChecker. Add the following code to the class:

class NetworkStatusChecker(private val connectivityManager: ConnectivityManager?) {

}

You need the connectivity manager, to check the current network information & its capabilities.

You’ll use this class in the following manner - you’ll check if there’s a good connection, and if there is, you’ll execute a certain action. Add the following code to achieve that:

class NetworkStatusChecker(private val connectivityManager: ConnectivityManager?) {

  inline fun performIfConnectedToInternet(action: () -> Unit) {
    if (hasInternetConnection()) {
      action()
    }
  }
}

So here, you’re checking if there is an Internet connection, and if there is, you’ll execute a given action. To finish this functionality, add the following code to the class:

class NetworkStatusChecker(private val connectivityManager: ConnectivityManager?) {

  inline fun performIfConnectedToInternet(action: () -> Unit) {
    if (hasInternetConnection()) {
      action()
    }
  }

  // HERE
  fun hasInternetConnection(): Boolean {
    val network = connectivityManager?.activeNetwork ?: return false
    val capabilities = connectivityManager.getNetworkCapabilities(network) ?: return false
  }
}

This will use the manager to get the information about the current network. If there is one, you fetch its capabilities, to see if it’s connected to a wifi network, mobile, or a VPN. If there isn’t a connection, you return false. Then finish the function like so:

class NetworkStatusChecker(private val connectivityManager: ConnectivityManager?) {

  inline fun performIfConnectedToInternet(action: () -> Unit) {
    if (hasInternetConnection()) {
      action()
    }
  }

  // HERE
  fun hasInternetConnection(): Boolean {
    val network = connectivityManager?.activeNetwork ?: return false
    val capabilities = connectivityManager.getNetworkCapabilities(network) ?: return false

    return capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)
        || capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)
        || capabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN)
  }
}

This will check if there is an active network, and if it’s a Wi-Fi, mobile, or VPN connection. That should give you enough info about the network, to conclude that the user is able to fetch data from a server.

This has been an important step but you still haven’t created any API calls. You’ll do that, in the next episode! :]