Instructional Student Lab Lesson Activity Guide Notebook: Kotlin Android App Network Applications

Learning Outcomes:
Network Connectivity
Set the Instructions here on how to get an API Key from OpenWeather:
− Connect to REST API
Representational State Transfer

− Get data from API: Method call in your code
JSON : JavaScript Object Notation: Data Description Language
Weather App, Currency Converter, Google Earth

− Parse data from API + Converting to object

− Display parsed data in UI

Kotlin Android App Development: Network Connectivity and REST API Integration


Welcome to this lecture on Kotlin Android app development, where we will explore the essential concepts and theory behind network connectivity, connecting to a REST API, getting data from an API, parsing data from an API, and displaying parsed data in a user interface.
By understanding these concepts, you will be able to build more complex and feature-rich Android applications in the future.

Network Connectivity

In modern mobile applications, it is essential to connect to the internet to access various resources and services.
Network connectivity in Android apps is achieved through the Android networking libraries, such as HttpURLConnection, OkHttp, and Retrofit.
These libraries facilitate communication between your app and remote servers using various protocols like HTTP and HTTPS.
SOAP: Simple Object Access Protocol


To access the internet, your app needs to request the necessary permissions in the AndroidManifest.xml file. The INTERNET permission is required to access the internet, while the ACCESS_NETWORK_STATE permission allows you to check the network status.

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

Connecting to a REST API

A REST (Representational State Transfer) API is a set of rules that allows you to interact with web services using HTTP methods (GET, POST, PUT, DELETE) to perform CRUD operations (Create, Read, Update, Delete).
In Android apps, we can use Retrofit, a popular HTTP client library, to simplify the process of connecting to REST APIs.


Retrofit is a type-safe HTTP client library for Android and Java. It allows you to define API service interfaces, create instances of these interfaces, and make API calls easily. Retrofit uses annotations to describe HTTP requests, making your code more readable and maintainable.

Getting Data From an API

When you interact with a REST API, you send an HTTP request and receive a response, usually in JSON or XML format.
To get data from an API, you need to perform an HTTP GET request using Retrofit.
To do this, you need to define an API service interface with the appropriate annotations specifying the endpoint and HTTP method.

Parsing Data From an API

Once you receive the data from the API, you must parse it into a more usable format, like objects, to manipulate and display it in your app.


JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write and easy for machines to parse and generate.
In Kotlin Android app development, you can use libraries like Gson or Moshi to parse JSON data into custom data classes.

Displaying Parsed Data in a UI

After parsing the data, the next step is to display it in your app's user interface. One common way to display lists of data in Android apps is by using a RecyclerView.


RecyclerView is a flexible and efficient Android UI component for displaying large sets of data in a scrolling list.
It efficiently recycles views to minimize memory usage and improve performance. To use a RecyclerView, you need to create an adapter that binds the data to the views and a layout manager that defines how the items are displayed.


In this lecture, we have covered the theory behind network connectivity, connecting to a REST API, getting data from an API, parsing data from an API, and displaying parsed data in a user interface.
Understanding these concepts will provide a strong foundation for building more complex and feature-rich Android applications in the future.

Lesson Overview

In this lab lesson activity,
you will learn how to develop an Android app using Kotlin with a focus on network connectivity. You will learn how to connect to a REST API, get data, parse it, convert it to objects, and display the parsed data in the user interface.


Basic understanding of Kotlin
Android Studio installed on your computer
Familiarity with Android app development

Lesson Objectives

By the end of this lesson, you will be able to:
Connect to a REST API using Kotlin and Android Studio
Get data from the API
Parse the data and convert it into objects
Display the parsed data in the user interface

Lesson Outline

1. Network Connectivity

1.1. Setting up Android Studio project

Launch Android Studio and create a new project.
Choose an Empty Activity template.
Name your project, set the package name, and choose Kotlin as the programming language.
Click "Finish" to create the project.

1.2. Adding internet permission

In the AndroidManifest.xml file, add the following permission: done
<uses-permission android:name="android.permission.INTERNET" />

2. Connect to REST API

2.1. Adding dependencies

In the app-level build.gradle file, add the following dependencies:
dependencies { implementation 'com.squareup.retrofit2:retrofit:2.9.0' implementation 'com.squareup.retrofit2:converter-gson:2.9.0'}

Sync your project.

2.2. Creating Retrofit instance

Create a new Kotlin file and name it "ApiClient".
In the ApiClient.kt file, add the following code: Main Activity

import retrofit2.Response;import retrofit2.Retrofitimport retrofit2.converter.gson.GsonConverterFactoryimport kotlin.reflect.KProperty
object ApiClient { private const val BASE_URL = ""
private val lazyRetrofit = lazy { Retrofit.Builder() .baseUrl(BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .build() }
operator fun getValue(nothing: Nothing?, property: KProperty<*>): Retrofit { return lazyRetrofit.value }}
Note: build.gradle (app)
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
) instead.