تعريف واجهة Retrofit API
صرّح بنقاط النهاية باستخدام التعليقات التوضيحية
تعريف واجهة Retrofit API درس مجاني في Jetpack Compose Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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/7) وفتح باقي دورة Jetpack Compose Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Jetpack Compose Academy 4 دروس في المجموع.
ماذا ستتعلم في «تعريف واجهة Retrofit API»؟
صرّح بنقاط النهاية باستخدام التعليقات التوضيحية تتمرن على Jetpack Compose Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Jetpack Compose Academy؟
لا تُشترط خبرة سابقة. Jetpack Compose Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «تعريف واجهة Retrofit API»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Jetpack Compose Academy هذا؟
نعم. كل درس في Jetpack Compose Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- تعريف واجهة Retrofit API
- تحليل JSON باستخدام Moshi
- استدعاءات suspend في ViewModel
- واجهة التحميل والنجاح والخطأ