This means you can create both iOS and Android applications with Kotlin and share a huge part of Kotlin code for both platforms. Ktor is built on Kotlin multi-platform mobile (KMM). Ktor client is designed for various platforms, such as Android, Native (iOS and desktop), JVM, and JavaScript. On the other hand, Ktor is an asynchronous HTTP client that runs on several platforms. Nevertheless, the Retrofit implementation in these platforms remains different. Retrofit is a java based Android library that can be used to develop Android and iOS based applications. One of the closest relatives of Ktor is Retrofit, which is also used to consume APIs in Android. Adding networking capabilities to an application developed with a traditional UI toolkit or Jetpack Compose is straightforward with the Ktor Client. We can use Ktor to make HTTP network requests to an API to get the response back to an application. Ktor can be compared to network library such as OkHttp and Retrofit. It is a modern asynchronous framework backed by Kotlin coroutines. But before we execute this request, we need to know the JSON response we should expect when it is executed successfully so that Retrofit can parse the JSON response and deserialize it to Java objects.Ktor is a client-server framework that helps us build applications in Kotlin.
We would like to make a POST request (create a new resource) on the API. We are going to create models automatically from the JSON response data by leveraging a very useful tool: jsonschema2pojo. To perform network operations, we need to include the INTERNET permission in the application manifest: AndroidManifest.xml. Make sure you sync your project after adding the dependencies. The dependencies include the Retrofit library and also Google's Gson library to convert JSON to POJO (Plain Old Java Objects) as well as Retrofit's Gson integration. 2. Declaring DependenciesĪfter creating a new project, declare the following dependencies in your adle.
We'll be using the API provided by JSONPlaceholder-this is a fake online REST API for testing and prototyping.Ĭheck out my previous post, Get Started With Retrofit 2 HTTP Client, to learn how to execute GET requests and how to integrate Retrofit with RxJava.įire up Android Studio and create a new project with an empty activity called MainActivity. I'll also show you how to integrate with RxJava and how to cancel requests.
In this tutorial, I will explain how to use Retrofit 2 to handle network requests by building a simple app that will perform POST requests, PUT requests (to update entities), and DELETE requests. Retrofit, on the other hand, is a well-planned, documented and tested library that will save you a lot of precious time and headaches. Simple Framework: 2:converter-simpleframeworkĭeveloping your own type-safe HTTP library to interface with a REST API can be a real pain: you have to handle many aspects, such as making connections, caching, retrying failed requests, threading, response parsing, error handling, and more.Instead, it ships support for the following JSON converter libraries to handle that: Also, Retrofit does not have a built-in JSON converter to parse from JSON to Java objects.
Behind the scenes, Retrofit makes use of OkHttp (from the same developer) to handle network requests. Like most open-source software, Retrofit was built on top of some other powerful libraries and tools. GET, POST, PUT, PATCH, and DELETE requests can all be executed. This powerful library makes it easy to consume JSON or XML data, which is then parsed into Plain Old Java Objects (POJOs). In this tutorial, I'll show you how to use one of the most popular and often recommended HTTP libraries available for Android. Retrofit makes it easy to connect to a REST web service by translating the API into Java interfaces.
Retrofit is a type-safe HTTP client for Android and Java.