Retrofit APIインターフェースを定義する
アノテーションでエンドポイントを宣言します。
「Retrofit APIインターフェースを定義する」はCoddyKit上の無料Jetpack Compose Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはJetpack Compose Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Jetpack Compose Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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.
よくある質問
「Retrofit APIインターフェースを定義する」レッスンは無料ですか?
はい。「Retrofit APIインターフェースを定義する」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Jetpack Compose Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Jetpack Compose Academyコースには全4レッスンが含まれています。
「Retrofit APIインターフェースを定義する」で何を学びますか?
アノテーションでエンドポイントを宣言します。 ブラウザで直接実行するハンズオンコードでJetpack Compose Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Jetpack Compose Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのJetpack Compose Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。
「Retrofit APIインターフェースを定義する」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このJetpack Compose Academyレッスンでコードを書いて実行できますか?
はい。すべてのJetpack Compose Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- Retrofit APIインターフェースを定義する
- MoshiでJSONを解析する
- ViewModel内のsuspend呼び出し
- 読み込み、成功、エラーのUI