Definir una interfaz de API de Retrofit
Declare endpoints con anotaciones.
Definir una interfaz de API de Retrofit es una lección gratuita de Jetpack Compose Academy en CoddyKit. Esta es la lección 1 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Jetpack Compose Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Jetpack Compose Academy incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en inglés.
What Retrofit Does
Retrofit turns a plain Kotlin interface into a working HTTP client, so you describe your API and it writes the networking code for you.
It Starts With an Interface
You declare each endpoint as a function inside a Kotlin interface. Retrofit reads the annotations and builds the real implementation at runtime.
interface UserApi {
// endpoint functions go here
}Mark a GET Request
The @GET annotation tells Retrofit this function fetches data. You pass the path that sits after your base URL. 🌐
@GET("users")
fun getUsers(): Call<List<User>>POST, PUT and DELETE Too
Beyond GET, Retrofit offers @POST, @PUT, and @DELETE so one interface can read and write data on your server.
@POST("users")
fun createUser(@Body user: User): Call<User>Path Parameters
Use @Path to inject a value into the URL. Retrofit swaps the named placeholder in braces for the argument you pass in.
@GET("users/{id}")
fun getUser(@Path("id") id: Int): Call<User>Query Parameters
Add @Query for values after the question mark, like search terms or page numbers. Retrofit appends them to the URL for you.
@GET("search")
fun search(@Query("q") term: String): Call<Result>The Return Type
The function's return type tells Retrofit what shape to expect back. A data class here maps straight onto the JSON response.
@GET("users/{id}")
fun getUser(@Path("id") id: Int): Call<User>Set the Base URL
You build one Retrofit instance with the base URL. Every endpoint path you declared is joined onto it automatically.
val retrofit = Retrofit.Builder()
.baseUrl("https://api.example.com/")
.build()Create the Implementation
Call create with your interface class and Retrofit returns a ready-to-use object. You never write the request code yourself.
val api = retrofit.create(UserApi::class.java)Add a Converter Factory
A converter factory teaches Retrofit how to turn JSON into your data classes. You add it once when building the instance.
.addConverterFactory(MoshiConverterFactory.create())Don't Forget the Permission
To reach the network, declare the INTERNET permission in your manifest. Without it, every request fails before it even starts.
<uses-permission android:name="android.permission.INTERNET" />Quick Check
Which annotation injects a value directly into the URL path?
Recap: Your API Interface
You described endpoints as annotated interface functions, set a base URL, added a converter, and let Retrofit build the client. Next you'll parse the JSON it returns.
Preguntas frecuentes
¿La lección «Definir una interfaz de API de Retrofit» es gratis?
Sí — el texto completo de «Definir una interfaz de API de Retrofit» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Jetpack Compose Academy, actualiza a CoddyKit PRO. El curso de Jetpack Compose Academy incluye 4 lecciones en total.
¿Qué aprenderé en «Definir una interfaz de API de Retrofit»?
Declare endpoints con anotaciones. Practicas Jetpack Compose Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar Jetpack Compose Academy?
No se requiere experiencia previa. Jetpack Compose Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 1 de 4.
¿Cuánto tiempo toma la lección «Definir una interfaz de API de Retrofit»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de Jetpack Compose Academy?
Sí. Cada lección de Jetpack Compose Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- Definir una interfaz de API de Retrofit
- Analizar JSON con Moshi
- Llamadas suspend en el ViewModel
- Interfaz de carga, éxito y error