0Pricing
Jetpack Compose Academy · Lezione

Definire un'interfaccia API Retrofit

Dichiari gli endpoint con le annotazioni.

Definire un'interfaccia API Retrofit è una lezione Jetpack Compose Academy gratuita su CoddyKit. Questa è la lezione 1 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Jetpack Compose Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Jetpack Compose Academy include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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.

Domande Frequenti

La lezione «Definire un'interfaccia API Retrofit» è gratuita?

Sì — il testo completo di «Definire un'interfaccia API Retrofit» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Jetpack Compose Academy, passa a CoddyKit PRO. Il corso Jetpack Compose Academy include 4 lezioni in totale.

Cosa imparerò in «Definire un'interfaccia API Retrofit»?

Dichiari gli endpoint con le annotazioni. Eserciti Jetpack Compose Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Jetpack Compose Academy?

Non è richiesta alcuna esperienza precedente. Jetpack Compose Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 1 di 4.

Quanto tempo richiede la lezione «Definire un'interfaccia API Retrofit»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Jetpack Compose Academy?

Sì. Ogni lezione Jetpack Compose Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Definire un'interfaccia API Retrofit
  2. Analizzare JSON con Moshi
  3. Chiamate suspend nel ViewModel
  4. UI di caricamento, successo ed errore
← Torna a Jetpack Compose Academy