#1 ์ด์ ๊ธ
[Android] Retrofit - ๊ธฐ์ด
#1 ์ด์ ๊ธ [Android] Retrofit - ๋ฐฐ๊ฒฝ๊ณผ ๊ตฌ์กฐ#1 Restrofit์ ๋ฐฐ๊ฒฝ#1-1 REST API REST API (REpresentational State Transfer Application Programming Interface)#1 ๋ฌด์(What)์ ๋ํ API์ธ๊ฐ?#1-1 ๊ฐ์REST(REpresentational State Transfer) ๋๋
kenel.tistory.com
์ ๊ฒ์๊ธ์ ์์ฑ๋ ์ฑ์ ์ผ๋ถ ์์ ํด์, Retrofit Instance๊ฐ ํต์ ๊ธฐ๋ก(Log)์ ๋จ๊ธฐ๊ฒ ๋ง๋ ๋ค. ๋, Retrofit Instance๊ฐ ์๋ฒ-ํด๋ผ์ด์ธํธ ๊ฐ ํต์ ์ ์ฑ๊ณต ๋๋ ์คํจ(Timeout)๋ก ํ๋จํ๋ ๊ท์น์ ๋ฐ๊ฟ๋ณธ๋ค.
#2 Interceptor
Interceptors - OkHttp
Interceptors Interceptors are a powerful mechanism that can monitor, rewrite, and retry calls. Hereโs a simple interceptor that logs the outgoing request and the incoming response. class LoggingInterceptor implements Interceptor { @Override public Respon
square.github.io
Retrofit์ OkHttp ๊ธฐ๋ฐ์ด๋ค. ๊ทธ๋ฆฌ๊ณ OkHttp ํด๋ผ์ด์ธํธ(๋ชจ๋)์๋ Interceptor๋ฅผ ๋ฌ ์ ์๋ค. ์ด Interceptor์ ์ญํ ์ ์๋ฒ-ํด๋ผ์ด์ธํธ ๊ฐ Request ๋ฐ Response๋ฅผ ๋ชจ๋ํฐ๋ง์ด๋ค.
#3 ์ฝ๋ ์์
#3-1 ๋ชจ๋ ์์ค build.gradle ์์
plugins {
...
}
android {
...
}
dependencies {
...
// Retrofit
...
// Coroutines
...
// ViewModel, LiveData
...
// OKHttp
implementation("com.squareup.okhttp3:logging-interceptor:5.0.0-alpha.6")
}
๋ชจ๋ ์์ค build.gradle์ dependencies { ... }์ Interceptor๋ฅผ ์ถ๊ฐํ๋ค.
#3-2 RetrofitInstance.kt ์์
// package com.example.loggingandtimeoutmanagement
import com.google.gson.GsonBuilder
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit
class RetrofitInstance {
companion object {
val BASE_URL = "https://jsonplaceholder.typicode.com/"
val interceptor = HttpLoggingInterceptor().apply {
this.level = HttpLoggingInterceptor.Level.BODY // ์์ฒญ(Request) ๋ฐ ์๋ต(Response)์ ๋ชจ๋ ๋ณธ๋ฌธ(Body) ๋ด์ฉ์ ๋ก๊ทธ๋ก ๊ธฐ๋กํ๊ฒ ๋ค๋ ์ค์
}
val client = OkHttpClient.Builder().apply {
this.addInterceptor(interceptor)
.connectTimeout(30, TimeUnit.SECONDS) // 30์ด๊ฐ ์๋ฒ์์ ํต์ (์ฐ๊ฒฐ์ํ ํ์ธ)์ ์๋, 30์ด ์ด๊ณผ ์ ํต์ ์คํจ๋ก ๊ฐ์ฃผ
.readTimeout(20, TimeUnit.SECONDS) // ์๋ฒ๊ฐ ํด๋ผ์ด์ธํธ์๊ฒ ๋ณด๋ด์ค๋ ๋ฐ์ดํฐ ํจํท(๋จ์) ๊ฐ ๋์ฐฉ ์๊ฐ์ด 20์ด๋ฅผ ์ด๊ณผํ๋ฉด ํต์ ์คํจ๋ก ๊ฐ์ฃผ (Response ๋ฐ๊ธฐ๋ฅผ ํฌ๊ธฐ)
.writeTimeout(25, TimeUnit.SECONDS) // ํด๋ผ์ด์ธํธ๊ฐ ์๋ฒ๋ก ๋ณด๋ด์ฃผ๋ ํจํท ๊ฐ ๋์ฐฉ ์๊ฐ์ด 25์ด๋ฅผ ์ด๊ณผํ๋ฉด ํต์ ์คํจ๋ก ๊ฐ์ฃผ (Response ๋ฐ๊ธฐ๋ฅผ ํฌ๊ธฐ)
}.build()
fun getRetrofitInstance(): Retrofit {
return Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create(GsonBuilder().create()))
.build()
}
}
}
์ด์ ๊ฐ์ด ์ฝ๋๋ฅผ ์์ ํ๋ฉด ๋๋ค. ๋จผ์ , HttpLogginInterceptor()๋ก ์ธํฐ์ ํฐ ๊ฐ์ฒด๋ฅผ ๋ง๋ค๊ณ , ์ด ์ธํฐ์ ํฐ๊ฐ ํต์ (Request ๋ฐ Response)์ ๋ชจ๋ ๋ณธ๋ฌธ(Body)๋ฅผ ๊ธฐ๋กํ๊ฒ ์ค์ ํ๋ค. ๊ทธ๋ฆฌ๊ณ OkHttpClient์ ์ด ์ธํฐ์ ํฐ๋ฅผ ๋ฌ์์ค๋ค.
Client์ ์ธํฐ์ ํฐ๋ฅผ ๋ค๋ ๊ฒ๊ณผ ๋ ๋ฆฝ์ ์ผ๋ก, Client์๋ connectTimeout, readTimeout, writeTimeout๋ ์ค์ ํด์ค ์ ์๋ค. Timeout์ ๋ํ ์์ธํ ์ ๋ณด๋ ์ฌ๊ธฐ์ ํ์ธํ๋ค. Timeout์ ๊ธฐ๋ณธ๊ฐ์ด ์ด๋ฏธ ์กด์ฌํ๋ฉฐ, ์ ์ฝ๋์ฒ๋ผ ์ฌ์ฉ์ ์ ์ํ ์ผ์ ๊ฑฐ์ ์๋ค๊ณ ํ๋ค. ํ์ง๋ง, ์ธํฐ๋ท ์๋๊ฐ ๊ต์ฅํ ๋๋ ค Timeout์ ๋๊ฒ ์ก์์ค ํ์๊ฐ ์๋ ์ํฉ์ด๋ผ๋ฉด ์ ์ ํ ๋ํ์ค ํ์๊ฐ ์๊ธด๋ค. ๋ฐ๋ ๊ทน๋จ์ผ๋ก Timeout์ ์์ฃผ ๋๊ฒ ์ก์๋ฒ๋ฆฌ๋ฉด, ์ฌ์ค์ ํต์ ๋ถ๋ฅ์ธ ์ํฉ์์๋ ์ค์ง ์์ ๋ฐ์ดํฐ ํจํท์ ๊ณ์ ๊ธฐ๋ค๋ฆฌ๋ฉฐ ๋๊ธฐํ๋ ๋ถ์์ฌ๊ฐ ๋ฐ์ํ ์ ์์ผ๋ ์ฃผ์ํด์ผ ํ๋ค.
#4 ์๋ ํ์ธ

์ ์คํฌ๋ฆฐ์ท๊ณผ ๊ฐ์ด OkHttp์ ํต์ ๋ก๊ทธ๋ฅผ ํ์ธํ ์ ์๋ค.
#5 ์์ฑ๋ ์ฑ
android-practice/retrofit/LoggingAndTimeoutManagement at master ยท Kanmanemone/android-practice
Contribute to Kanmanemone/android-practice development by creating an account on GitHub.
github.com
'๊นจ์ ๊ฐ๋ ๐ > Android' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Android] Notifications - ๊ธฐ์ด (0) | 2024.06.12 |
---|---|
[Android] Retrofit - Post (0) | 2024.06.11 |
[Android] Retrofit - MVVM ๊ตฌ์กฐ (0) | 2024.06.05 |
[Android] Retrofit - ๊ธฐ์ด (0) | 2024.05.29 |
[Android] Retrofit - ๋ฐฐ๊ฒฝ๊ณผ ๊ตฌ์กฐ (0) | 2024.05.28 |