kTor Android Client Tutorial – Performing Simple GET Request

Previously I’ve published a tutorial about building a RESTful API with kTor. But you know what kTor is multiplatform, that means we can also consume the RESTful APIs using kTor; and in this kTor Android …

kTor Android Client Tutorial

Previously I’ve published a tutorial about building a RESTful API with kTor. But you know what kTor is multiplatform, that means we can also consume the RESTful APIs using kTor; and in this kTor Android Client Tutorial we will learn performing a simple HTTP GET request using ktor android client.

Check the updated kotlin ktor android client tutorial here. 

In this project I will build a movie list application as shown below.

ktor android client
ktor android client tutorial

TMDB API

To get the list of movies I am using TMDB API here. To create this project you will require a TMDB API Key to authenticate the api, so signup for a TMDB Account here and get your api key first.

Get the Starter Project

Before moving ahead you need my starter project. You can clone the project from this repository. Make sure you are in the branch 1_starter_project  of this repository.

This project contains the UI Design, Basic Setup and all the dependencies. As this tutorial is about ktor android client, below you can see the specific dependencies that are required to add ktor android client in your project.

The starter project contains many other dependencies as well; for example hilt, coroutines etc.

Once you have cloned the project (make sure you are at 1_starter_project  branch) run the project to make sure everything is working fine.

ktor android client
starter project

If you are getting the result as shown in the above screenshot you can move ahead.

Creating an HTTP Client (kTor Android Client)

Let’s create a class named TmdbHttpClient , and this class will contain a function getHttpClient()  and this function will return the HttpClient .

The code is pretty much self explanatory, in case you have any problem understanding it let’s discuss it in the comments below.

Make sure you have added your tmdb api key in place of TmdbApiKeys.API_KEY  in the above code.

TMDB API URL

Create one more file, I’ve created a file named BaseUrl.kt  and here I’ve defined the TMDB API Base URL.

Creating a Resource Wrapper

Now, I will create a Resource Wrapper. The reason of this wrapper is, when we perform an api call we may get an error as well. Also while performing the api we need to show a progressbar in the UI as well. So we can see we have three states for the api result; Loading, Success and Failure. We need the wrapper to maintain these three states.

To create the wrapper we can use sealed class.

Model Classes

I forget to tell you that, what api we are going to use in this project. So I am going to use the Popular Movies API.

If you go to the api doc, you can see this api will return the following result.

tmdb popular movies api
tmdb popular movies api

So we will create two classes to map the above result into kotlin data class.

First create a class for Movie .

Then we will create one more class named PopularMovies .

Creating Movie Repository

Now let’s create a repository interface. The implementation of this interface will perform the network request.

We need only one function here that will get the popular movies from the tmdb api. Now let’s define the implementation of this interface.

We have the repository ready that will perform the actual api request.

Hilt Module

Now let’s define a hilt module that will provide the http client and repository.

Creating ViewModel

Let’s create a ViewModel now. The ViewModel will consume the repository to get the popular movies data from the API.

Now we can simply collect the movies flow to render the movies list in the UI.

Displaying Movie List

For this project I am using Jetpack Compose and the UI related code is already there in the starter project; you just need to make few modifications to display the movie list that we got from the api call.

The MainActivity.kt will be like.

Then modifie the MovieList()  composable.

Also the MovieItem()  composable.

And that’s it, now you can try running the application and it should work.

The complete code for this project is in the branch 2_final_project ; you can switch to this branch in the same project to get the complete source code.

So that is all for this kTor Android Client Tutorial; in case you have any question or confusion you can leave it in the comments below. Thank You 🙂

 

Hi, my name is Belal Khan and I am a Google Developers Expert (GDE) for Android. The passion of teaching made me create this blog. If you are an Android Developer, or you are learning about Android Development, then I can help you a lot with Simplified Coding.

Expand Your Knowledge: Next Tutorial Picks

0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Harsh
Harsh
9 months ago

JsonFeature is not found while creating client. May I know from where this attribute is calling from ?

Deepesh
Deepesh
9 months ago

How To integrate Ktor in java based application

3
0
Would love your thoughts, please comment.x
()
x