Skip to main content

Posts

Showing posts with the label Ktor

jetpack compose ktor - How to post data

KtorClient.kt package com.cfsuman.composenetwork import android.util.Log import io.ktor.client.* import io.ktor.client.features.* import io.ktor.client.features.json.* import io.ktor.client.features.json.serializer.* import io.ktor.client.features.logging.* import io.ktor.client.request.* import io.ktor.http.* import kotlinx.serialization.json.Json object KtorClient{ private val json = Json { encodeDefaults = true ignoreUnknownKeys = true } val httpClient = HttpClient { install(JsonFeature){ serializer = KotlinxSerializer(json) } install(Logging){ logger = object : Logger { override fun log(message: String) { Log.d("xapp-ktor", message ) } } level = LogLevel.ALL } install(HttpTimeout){ socketTimeoutMillis = 15_000 requestTimeoutMillis = 15_000 connectTimeoutMill...

Jetpack Compose Ktor: How to pass parameters

Introduction In Android development, networking is a key aspect of building apps that communicate with external services, and Ktor—a Kotlin-based HTTP client—is a popular choice for handling network requests in modern apps. Combined with Jetpack Compose, Android's declarative UI toolkit, we can achieve clean, readable, and maintainable code. This example demonstrates how to pass parameters to an API call using Ktor in an Android Jetpack Compose project. We'll break down the components of the project, focusing on network requests, parameter passing, and the integration of Compose for displaying API responses. This guide will take you through the steps of creating a simple Android app that makes a network request using Ktor and displays the response. You will see how Ktor's features like JSON serialization, logging, and timeouts are configured, and how parameters are passed in the network request. By the end, you'll understand how to make asynchronous network calls in a C...

Jetpack Compose Ktor: How to get API data

Introduction In modern Android development, Jetpack Compose and Ktor make it easier to build user interfaces and interact with APIs, respectively. Jetpack Compose simplifies UI development with a declarative approach, while Ktor provides an efficient way to perform HTTP requests. In this article, we'll explore how to combine these two powerful tools to fetch data from an API using Ktor and display it in a Jetpack Compose UI. The example involves creating an Android application that fetches a list of users from a placeholder API and presents it in a scrollable list format. The project consists of three main components: the KtorClient to handle network requests, a function to fetch user data from an API, and the MainActivity that sets up the Jetpack Compose UI to display the fetched data. Each component plays a crucial role in ensuring that the app communicates effectively with the API and presents the data in a user-friendly manner. KtorClient Setup The KtorClient.kt file config...