![]() Just to clarify HttpHeaders.Authorization is nothing but a string with value “Authorization” provided inbuilt by Ktor. The lambda function is HttpRequestBuilder where we have specified our header, which in our case is Authorization and our url. Http.get specifies that we are making a GET request to our API, we can use Http.post to make a POST request. Ktor-Client does not use the reflection APIs or OkHttp, instead it uses an asynchronous client to perform HTTP requests named HttpClient which uses HttpResponsePipeline and HttpRequestPipeline, former to process all the responses sent by the server and latter for processing all the requests sent by the client. However, if you want to check the complete working code on GitHub, it can be found here. I will be using the MVVM architecture and skipping the UI and the ViewModel part as it is out of scope of this article. We will be adding popular unsplash API in our project with authentication to our header. It is developed by JetBrains, so they will keep adding more and more features in coming time.Įnough talking and theory!! Let’s now jump straightaway to the coding part. With the popularity of Kotlin Multiplatform and it being adopted by various companies, especially startups, I am positive that it has a chance of replacing Retrofit in coming years. Moreover it is as easy to add headers, serialization, logging etc in our app, as it is with Retrofit. Ktor uses coroutines internally for asynchronous programming which makes our code very clean and minimal just like coroutines did with suspend functions. Besides this, knowledge and familiarity with Ktor will also help us developing APIs as discussed in my previous article here This knowledge of using Ktor-Client will not only help in developing for Android, but also developing multiplatform projects. Ktor-Client can not only be used for networking in native Android specific applications, but also when we are working on multi-platform Kotlin projects, unlike Retrofit which is platform-specific. Kotlin Multiplatform is trending especially after Google launched a stable version of Jetpack Compose last year. But how about we want to use something similar in other platforms as well? When I say other platforms, I bet Kotlin Multiplatform comes to your mind as one of the options as well. Retrofit is great when we want to use a networking library for our Android native apps. ![]() Now the next question arises, why? Why do we need a new networking library when we already have Retrofit? ![]() In simple words, Ktor-Client, just like Retrofit can be used to make API calls, with functionalities such as adding headers, choosing from various serialization libraries and much more. Ktor includes a multiplatform asynchronous HTTP client, which allows you to make requests and handle responses, extend its functionality with plugins (formerly known as features), such as authentication, JSON serialization, and so on. Import ’s check out first what Ktor-Client is. Why am I leaking coroutines here? package dummy My understanding is that runBlocking will create a scope and that it will block as long as there are coroutines living inside that scope (or child scopes), but I when the call to runBlocking in the test below returns there are still two coroutines alive. I'm trying to wrap my head around Kotlin coroutines and Ktors websocket support. How can I make sure all Kotlin coroutines created by a ktor websocket client are cleared up? - How can I make sure all Kotlin coroutines created by a ktor websocket client are cleared up?
0 Comments
Leave a Reply. |