Learning Android Development

Kotlin Ktor Network Fetching on Android

Preparing network fetching code that works on Android and iOS

Image from Pixabay by TheDigitalArtist

In my last article, I wrote about various ways of combining iOS and Android development together. In it, I introduced Kotlin Multiplatform Mobile (KMM). It enables iOS and Android to share common code, while still leveraging each platform-specific code (e.g. SwiftUI & Jetpack Compose).

To explore further, I want to check if I can have a Common Code of Network Fetching. Initially, I thought of just using OkHttp, the defacto network fetching framework for Android. However, I think it not possible for now (Let me know if it could).

The good news is, the possible way of sharing network fetch across platforms. is using the Kotlin Network Fetching framework, Ktor. In this article, I’ll share the basic of how to perform network fetch with Ktor in Android (will explore multiplatform later).

Example design

To demo the code of Ktor, I have made a small app with code in Github. It is as below, which has both example in OkHttp and Ktor way of fetching data from Wikipedia.

--

--