Networking with Retrofit
Almost every app talks to a server. Retrofit turns a simple annotated Kotlin interface into a fully working, type-safe HTTP client — and with coroutines, network calls read like ordinary code.
Learn Networking with Retrofit in our free Kotlin course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…
Part of the free Kotlin course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
By the end you'll define an API interface with @GET / @POST and suspend functions, plug in a converter, and meet the Ktor client alternative.
What You'll Learn in This Lesson
1️⃣ Describing the API
You declare endpoints on an interface using annotations. @GET sets the method and path; @Path fills URL placeholders; @Query adds query parameters. Mark calls suspend so they run in a coroutine.
The interface is pure description. Retrofit generates the working client from it at runtime.
2️⃣ Building Retrofit and Making a Call
A Retrofit.Builder sets the base URL and a converter (here a kotlinx.serialization Json converter), then create() returns a live implementation.
3️⃣ The Ktor Client Alternative
Ktor 's HttpClient is a Kotlin-first, multiplatform alternative. It uses suspend functions and a content-negotiation plugin for JSON.
Your turn. Replace the TODO , then reason about the URL.
Add a search method with a query parameter and a list return type.
📋 Quick Reference — Retrofit
Practice quiz
In Retrofit, you describe an API as what?
- A data class
- A Kotlin interface with annotated methods
- An XML file
- An abstract class with bodies
Answer: A Kotlin interface with annotated methods. Retrofit turns an annotated interface into a working HTTP client implementation.
Which annotation maps a method to an HTTP GET request?
- @Get
- @HttpGet
- @GET
- @Fetch
Answer: @GET. @GET("path") marks a method as an HTTP GET to that path.
Which annotation is used for sending data to create a resource?
- @POST
- @SEND
- @PUSH
- @PUT only
Answer: @POST. @POST("path") issues an HTTP POST, typically with a @Body payload.
Why mark a Retrofit method as 'suspend'?
- To make it synchronous
- So it can be called from a coroutine and not block the main thread
- To cache results
- To make it private
Answer: So it can be called from a coroutine and not block the main thread. A suspend function lets Retrofit run the call asynchronously from a coroutine.
What does a converter (e.g. a Json converter) do in Retrofit?
- Encrypts traffic
- Converts between HTTP bodies and your Kotlin objects
- Manages the back stack
- Schedules background jobs
Answer: Converts between HTTP bodies and your Kotlin objects. A converter serializes request bodies and deserializes responses into your data classes.
How do you supply a path parameter like /users/{id} in Retrofit?
- With @Query
- With @Header
- With @Field
- With @Path("id")
Answer: With @Path("id"). @Path("id") binds a method parameter into the {id} placeholder in the URL.
How do you add a query parameter such as ?page=2?
- @Query("page")
- @Path("page")
- @Body
- @Url
Answer: @Query("page"). @Query("page") appends a query string parameter to the request URL.
What does Retrofit.Builder().baseUrl(...).build().create(Api::class.java) return?
- A raw JSON string
- A generated implementation of your Api interface
- A coroutine scope
- A database
Answer: A generated implementation of your Api interface. create() returns a working implementation of the annotated interface.
What is Ktor client in this context?
- A UI toolkit
- A multiplatform HTTP client, an alternative to Retrofit
- A serialization format
- An Android Activity
Answer: A multiplatform HTTP client, an alternative to Retrofit. Ktor's HttpClient is a Kotlin-first, multiplatform alternative to Retrofit for networking.
Where should you typically launch a network call in an Android app?
- On the main thread directly
- In onCreate synchronously
- In a coroutine, e.g. within viewModelScope
- In the manifest
Answer: In a coroutine, e.g. within viewModelScope. Network calls run in coroutines (often viewModelScope) so they don't block the UI thread.