Ein Retrofit-API-Interface definieren
Deklarieren Sie Endpunkte mit Annotations.
Ein Retrofit-API-Interface definieren ist eine kostenlose Jetpack Compose Academy-Lektion auf CoddyKit. Dies ist Lektion 1 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Jetpack Compose Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Jetpack Compose Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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.
Häufig gestellte Fragen
Ist die Lektion „Ein Retrofit-API-Interface definieren“ kostenlos?
Ja — der vollständige Text von „Ein Retrofit-API-Interface definieren“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Jetpack Compose Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Jetpack Compose Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Ein Retrofit-API-Interface definieren“?
Deklarieren Sie Endpunkte mit Annotations. Du übst Jetpack Compose Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Jetpack Compose Academy zu starten?
Keine Vorkenntnisse erforderlich. Jetpack Compose Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 1 von 4.
Wie lange dauert die Lektion „Ein Retrofit-API-Interface definieren“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Jetpack Compose Academy-Lektion Code schreiben und ausführen?
Ja. Jede Jetpack Compose Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Ein Retrofit-API-Interface definieren
- JSON mit Moshi parsen
- suspend-Aufrufe im ViewModel
- UI für Laden, Erfolg und Fehler