Thuta Learning
ရှာဖွေရန်
AdvancedMobile Developmentintermediate

Networking (Retrofit)

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Networking (Retrofit) ကို ကြောက်စရာမလိုအောင် နားလည်မယ်
  • ကိုယ်တိုင် Android Studio/Compose code ကို run ကြည့်တတ်မယ်
  • Real project ထဲမှာ ဒီ concept ကို ချက်ချင်း အသုံးချတတ်မယ်

ခဏလေး ဒီလိုပဲ စဉ်းစားကြည့်

Retrofit က Android မှာ REST API call လုပ်ဖို့ အသုံးအများဆုံး library ပါ — API endpoint (URL, HTTP method) ကို Kotlin interface အနေနဲ့ declare လုပ်ရုံနဲ့ (annotation ဖြင့်), Retrofit က HTTP request/response ကို ကိုင်တွယ်ပေးပါတယ်၊ JSON response ကို Kotlin data class အဖြစ်ပါ auto-convert ပေးပါတယ် (Gson/Moshi converter ဖြင့်)။ API call တွေက network ကို depend ပါတယ်ဆိုတော့ asynchronous ဖြစ်ရပါတယ် — Kotlin Coroutines (နောက် lesson) နဲ့ တွဲသုံးပါတယ်.

လက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်

`interface WeatherApi { @GET("weather") suspend fun getWeather(@Query("city") city: String): WeatherResponse }` လို့ define လုပ်ပြီး, `retrofit.create(WeatherApi::class.java)` နဲ့ implementation ကို auto-generate ပေးပါတယ် — `viewModelScope.launch { val weather = api.getWeather("Yangon") }` လို့ ခေါ်ရင် API ကနေ weather data ကို fetch ယူပြီး UI ထဲ ပြပေးနိုင်ပါတယ်.

အတူတူ ကြည့်မယ်

kotlin
data class WeatherResponse(val city: String, val tempCelsius: Double)

interface WeatherApi {
    @GET("weather")
    suspend fun getWeather(@Query("city") city: String): WeatherResponse
}

val retrofit = Retrofit.Builder()
    .baseUrl("https://api.example.com/")
    .addConverterFactory(GsonConverterFactory.create())
    .build()
val weatherApi = retrofit.create(WeatherApi::class.java)
You should see
$ val weather = weatherApi.getWeather("Yangon")
// weather: WeatherResponse(city="Yangon", tempCelsius=32.0)

၅ မိနစ် စမ်းကြည့်

`WeatherApi` interface ကို ကိုယ်တိုင် define လုပ်ကြည့်ပါ (public free weather API တစ်ခုကို ရွေးချယ်ပြီး) — Retrofit instance ဆောက်ပြီး API call တစ်ခု (documentation review, dry-run) ကို ဒီဇိုင်းရေးကြည့်ပါ။

သတိလေးတစ်ချက်

API key (secret) ကို app code ထဲ hardcode ရေးထားပြီး APK ထဲ ပါသွားရင်, decompile လုပ်ပြီး key ကို ထုတ်ယူနိုင်ပါတယ် — sensitive key ကို backend proxy server ကနေတစ်ဆင့် သုံးသင့်ပါတယ်။

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • Network call ကို main thread (UI thread) ပေါ်မှာ တိုက်ရိုက် run ခြင်း — app freeze/crash (NetworkOnMainThreadException) ဖြစ်နိုင်ပါတယ် (suspend function + Coroutines သုံးမှ ရှောင်လွှဲနိုင်ပါတယ်)
  • API call ရဲ့ error/failure case (network down, timeout) ကို handle မလုပ်ဘဲ success case ကိုသာ ရေးထားခြင်း — production app မှာ try/catch ဒါမှမဟုတ် Result wrapper လိုအပ်ပါတယ်

အခု ကိုယ်တိုင် စမ်းကြည့်

`WeatherApi` interface ကို ကိုယ်တိုင် define လုပ်ကြည့်ပါ (public free weather API တစ်ခုကို ရွေးချယ်ပြီး) — Retrofit instance ဆောက်ပြီး API call တစ်ခု (documentation review, dry-run) ကို ဒီဇိုင်းရေးကြည့်ပါ။

You'll know it worked when: $ val weather = weatherApi.getWeather("Yangon") // weather: WeatherResponse(city="Yangon", tempCelsius=32.0)

Networking (Retrofit) | Thuta Learning