How to use Ktor client on Android

Walking to Kotlin multiplatform using Ktor Client

Juan Guillermo Gómez Torres
Google Developer Experts

--

https://github.com/ktorio/ktor

Ktor is an asynchronous open source framework for creating microservices and web applications. It was developed with Kotlin by Jetbrains. It’s easy to install and use, It’s extensible if you want to add a step to the execution pipeline. Its asynchronous property allows it to receive multiple requests thanks to coroutines and it is also multiplatform.

Ktor is multiplatform, yes multiplatform! It means that we can deploy Ktor applications anywhere, and not only as a server, but we can also consume microservices on Android, iOS, or JS with Ktor Client. Ktor Client allows you to make requests and handle responses, extend its functionality with features, such as authentication, JSON serialization, and so on.

We will learn how to configure, how to create client instances, and how to consume the REST API.

Let’s start by including the following dependencies in the build gradle.

The first dependency is the engine that handles network requests, we will use the android client for this case. The second dependency is used for JSON serialization and deserialization settings and is recommended for multiplatform projects. The third dependency is kotlinx.serialization, which is used for entity serialization. Finally, the fourth is for logging HTTP requests.

Now, we continue to include kotlinx.serialization plugin in the project gradle.

Now let’s configure the engine and create the client instance.

Depending on the engine (Android, Apache, OKHttp, etc), you could have different properties. We did the following in this configuration:

  • First, we configure the JSON serializer / deserializer with kotlinx.serialization by adding various properties. We also define a timeout period.
  • Second, we configure the logging to create HTTP request logs.
  • Third, we configure an observer that will record all the states of the responses.
  • Finally, we can set default values for each HTTP request. In this case, we define properties in the header, these will go in all requests.

Done! Ktor is now configured and the instance is ready to use. Keep calm, this is a one-time job. Let‘s use it!

We start to define the entity:

We continue configuring the UserApi:

We continue configuring the repository:

Finally, we will handle exceptions with an extension function:

UPDATES — Testing a Client

If you need to create unit tests for methods that use the Ktor client, you should use the MockEngine class provided by the framework that allows you to define a response with the required content.

An instance of MockEngine is first created and then passed as a parameter to the HttpClient class.

Testing a Ktor client

Congrats! We have configured, created the client, and created requests to an API with Ktor. The advantages are that It is simple to implement, it is multiplatform, it uses coroutines internally, it doesn’t use annotations and It has a variety of configuration options, depending on the use case and requirements. One downside could be that it is a bit more verbose to set up and use than other ways.

I hope you find it useful, remember to share this article.

Any comments are always welcome!

Resources

--

--

Juan Guillermo Gómez Torres
Google Developer Experts

Tech Lead in Wordbox. @GDGCali Founder. @DevHackCali Founder. Firebase & GCPcloud & Kotlin & ODML @GoogleDevExpert . Android lover.