์ถ์ฒ
ChatGPT
Retrofit2์์ ํ์์์ ๋ฐ ๋ฆฌํธ๋ผ์ด ์ค์
1. OkHttpClient๋ก ํ์์์ ์ค์ ํ๊ธฐ
OkHttpClient๋ ์ธ ๊ฐ์ง ์ข ๋ฅ์ ํ์์์์ ์ค์ ํ ์ ์๋ค.
- ์ฐ๊ฒฐ ํ์์์(connect timeout): ์๋ฒ์ ์ฐ๊ฒฐํ๋ ๋ฐ ๊ฑธ๋ฆฌ๋ ์ต๋ ์๊ฐ.
- ์ฝ๊ธฐ ํ์์์(read timeout): ์๋ฒ์์ ์๋ต ๋ฐ์ดํฐ๋ฅผ ์ฝ๋ ๋ฐ ๊ฑธ๋ฆฌ๋ ์ต๋ ์๊ฐ.
- ์ฐ๊ธฐ ํ์์์(write timeout): ์๋ฒ์ ์์ฒญ ๋ฐ์ดํฐ๋ฅผ ์ฐ๋ ๋ฐ ๊ฑธ๋ฆฌ๋ ์ต๋ ์๊ฐ.
๊ฐ๊ฐ์ ํ์์์์ ํน์ ์๊ตฌ ์ฌํญ์ ๋ฐ๋ผ ๊ฐ๋ณ์ ์ผ๋ก ์ค์ ํ ์ ์๋ค.
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import java.util.concurrent.TimeUnit;
public class ApiClient {
private static final String BASE_URL = "https://api.example.com/";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit == null) {
// OkHttpClient ์ค์
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS) // ์ฐ๊ฒฐ ํ์์์: 30์ด
.readTimeout(30, TimeUnit.SECONDS) // ์ฝ๊ธฐ ํ์์์: 30์ด
.writeTimeout(30, TimeUnit.SECONDS) // ์ฐ๊ธฐ ํ์์์: 30์ด
.build();
// Retrofit ์ค์
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
์ค๋ช
- connectTimeout: ์๋ฒ์ ์ฐ๊ฒฐํ ๋์ ํ์์์์ ์ค์ ํ๋ค.
- readTimeout: ์๋ฒ๋ก๋ถํฐ ๋ฐ์ดํฐ๋ฅผ ์ฝ์ ๋์ ํ์์์์ ์ค์ ํ๋ค.
- writeTimeout: ์๋ฒ๋ก ๋ฐ์ดํฐ๋ฅผ ๋ณด๋ผ ๋์ ํ์์์์ ์ค์ ํ๋ค.
- TimeUnit.SECONDS: ํ์์์ ์๊ฐ์ ๋จ์๋ฅผ ์ค์ ํ๋ค. (TimeUnit.MILLISECONDS ๋ฑ ๋ค๋ฅธ ๋จ์๋ ์ฌ์ฉ ๊ฐ๋ฅ)
๋๋ณด๊ธฐ
์ฝํ๋ฆฐ ์์
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit
object ApiClient {
private const val BASE_URL = "https://api.example.com/"
private var retrofit: Retrofit? = null
fun getClient(): Retrofit {
if (retrofit == null) {
// OkHttpClient ์ค์
val client = OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS) // ์ฐ๊ฒฐ ํ์์์: 30์ด
.readTimeout(30, TimeUnit.SECONDS) // ์ฝ๊ธฐ ํ์์์: 30์ด
.writeTimeout(30, TimeUnit.SECONDS) // ์ฐ๊ธฐ ํ์์์: 30์ด
.build()
// Retrofit ์ค์
retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build()
}
return retrofit!!
}
}
2. ๋ฆฌํธ๋ผ์ด ํ์ ์ค์
OkHttpClient๋ ๊ธฐ๋ณธ์ ์ผ๋ก ์๋์ผ๋ก ์ฌ์๋๋ฅผ ์ง์ํ์ง ์์ง๋ง, ์ด๋ฅผ ๊ตฌํํ ์ ์๋ค. ๋ฆฌํธ๋ผ์ด ๊ธฐ๋ฅ์ Interceptor๋ฅผ ์ฌ์ฉํด ๊ตฌํํ ์ ์๋ค.
๋ฆฌํธ๋ผ์ด ์ธํฐ์ ํฐ ๊ตฌํ
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
public class ApiClient {
private static final String BASE_URL = "https://api.example.com/";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit == null) {
// ๋ฆฌํธ๋ผ์ด ์ธํฐ์
ํฐ ๊ตฌํ
Interceptor retryInterceptor = new Interceptor() {
private int maxRetry = 3; // ์ต๋ ๋ฆฌํธ๋ผ์ด ํ์ ์ค์
private int retryCount = 0;
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Response response = null;
boolean responseOK = false;
while (!responseOK && retryCount < maxRetry) {
try {
response = chain.proceed(request);
responseOK = response.isSuccessful();
} catch (Exception e) {
retryCount++;
if (retryCount >= maxRetry) {
throw e; // ์ต๋ ๋ฆฌํธ๋ผ์ด ํ์๋ฅผ ์ด๊ณผํ๋ฉด ์์ธ ๋ฐ์
}
}
}
return response;
}
};
// OkHttpClient ์ค์
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.addInterceptor(retryInterceptor) // ๋ฆฌํธ๋ผ์ด ์ธํฐ์
ํฐ ์ถ๊ฐ
.build();
// Retrofit ์ค์
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
์ค๋ช
- maxRetry: ์ต๋ ๋ฆฌํธ๋ผ์ด ํ์๋ฅผ ์ค์ ํฉ๋๋ค.
- retryCount: ํ์ฌ ๋ฆฌํธ๋ผ์ด ํ์๋ฅผ ์ถ์ ํฉ๋๋ค.
- response.isSuccessful(): ์๋ต์ด ์ฑ๊ณต์ ์ธ์ง ํ์ธํฉ๋๋ค. ์ฑ๊ณต์ ์ด์ง ์์ผ๋ฉด ์ฌ์๋๋ฅผ ๊ณ์ํฉ๋๋ค.
- chain.proceed(request): ์์ฒญ์ ์ฒ๋ฆฌํฉ๋๋ค. ์์ฒญ์ด ์คํจํ๋ฉด ์์ธ๊ฐ ๋ฐ์ํ ์ ์์ต๋๋ค.
- throw e: ์ต๋ ๋ฆฌํธ๋ผ์ด ํ์๋ฅผ ์ด๊ณผํ๋ฉด ์์ธ๋ฅผ ๋ฐ์์ํต๋๋ค.
๋๋ณด๊ธฐ
์ฝํ๋ฆฐ ์์
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.io.IOException
import java.util.concurrent.TimeUnit
object ApiClient {
private const val BASE_URL = "https://api.example.com/"
private var retrofit: Retrofit? = null
fun getClient(): Retrofit {
if (retrofit == null) {
// ๋ฆฌํธ๋ผ์ด ์ธํฐ์
ํฐ ๊ตฌํ
val retryInterceptor = Interceptor { chain ->
val request = chain.request()
var response: Response? = null
var responseOK = false
var tryCount = 0
val maxRetry = 3 // ์ต๋ ๋ฆฌํธ๋ผ์ด ํ์ ์ค์
while (!responseOK && tryCount < maxRetry) {
try {
response = chain.proceed(request)
responseOK = response.isSuccessful
} catch (e: IOException) {
tryCount++
if (tryCount >= maxRetry) {
throw e // ์ต๋ ๋ฆฌํธ๋ผ์ด ํ์๋ฅผ ์ด๊ณผํ๋ฉด ์์ธ ๋ฐ์
}
}
}
response!!
}
// OkHttpClient ์ค์
val client = OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.addInterceptor(retryInterceptor) // ๋ฆฌํธ๋ผ์ด ์ธํฐ์
ํฐ ์ถ๊ฐ
.build()
// Retrofit ์ค์
retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build()
}
return retrofit!!
}
}
3. ์ฌ์ฉ์ ์ง์ OKHttpClient ์ฌ์ฉํ๊ธฐ
Retrofit2๋ฅผ ๊ตฌ์ฑํ ๋,OkHttpClient๋ฅผ ์ปค์คํฐ๋ง์ด์งํด ํ์์์๊ณผ ๋ฆฌํธ๋ผ์ด ์ค์ ์ ๋ณด๋ค ์ธ๋ถ์ ์ผ๋ก ๊ด๋ฆฌํ ์ ์๋ค.
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
public class ApiClient {
private static final String BASE_URL = "https://api.example.com/";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit == null) {
// ์ฌ์ฉ์ ์ง์ OkHttpClient
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.retryOnConnectionFailure(true) // ์ฐ๊ฒฐ ์คํจ ์ ์๋ ์ฌ์๋
.build();
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
๋๋ณด๊ธฐ
์ฝํ๋ฆฐ ์์
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit
object ApiClient {
private const val BASE_URL = "https://api.example.com/"
private var retrofit: Retrofit? = null
fun getClient(): Retrofit {
if (retrofit == null) {
// ์ฌ์ฉ์ ์ง์ OkHttpClient
val client = OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.retryOnConnectionFailure(true) // ์ฐ๊ฒฐ ์คํจ ์ ์๋ ์ฌ์๋
.build()
// Retrofit ์ค์
retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build()
}
return retrofit!!
}
}
์ค๋ช
- retryOnConnectionFailure(true): ์ฐ๊ฒฐ ์คํจ ์ ์๋์ผ๋ก ์ฌ์๋๋ฅผ ์ํํฉ๋๋ค.
- OkHttpClient.Builder(): ์ฌ์ฉ์ ์ง์ OkHttpClient ๋น๋๋ฅผ ์ฌ์ฉํ์ฌ ์ธ๋ถ ์ค์ ์ ํฉ๋๋ค.
๊ฒฐ๋ก
- ํ์์์ ์ค์ : ์ฐ๊ฒฐ, ์ฝ๊ธฐ, ์ฐ๊ธฐ ํ์์์์ ๊ฐ๊ฐ ์ค์ ํ์ฌ ๋ค์ํ ๋คํธ์ํฌ ์ํฉ์ ๋์ฒํ ์ ์๋ค. OkHttpClient๋ฅผ ์ฌ์ฉํ๋ค.
- ๋ฆฌํธ๋ผ์ด ์ค์ : Interceptor๋ฅผ ์ฌ์ฉํ์ฌ ์๋ ๋ฆฌํธ๋ผ์ด ๊ธฐ๋ฅ์ ๊ตฌํํ ์ ์์ผ๋ฉฐ, ํ์ํ ๊ฒฝ์ฐ ์ฌ์ฉ์ ์ง์ ๋ก์ง์ ์ถ๊ฐํ ์ ์๋ค.
'๋น ๊ตฌ๋ฉ ์ฑ์ฐ๊ธฐ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Android] Retrofit2 ๊ธฐ๋ณธ ์ค๋ช (0) | 2024.07.29 |
---|---|
[Retrofit2] Retrofit2์์ API ํธ์ถ ์ ๊ณตํต ํค๋ ๊ฐ ์ถ๊ฐ ๋ฐฉ๋ฒ (0) | 2024.07.29 |
[RESTful API]RESTful API๋ (0) | 2024.07.29 |
[Android][ART] ART์ ํ๋กํ์ผ ๊ธฐ๋ฐ ์ปดํ์ผ (0) | 2024.07.26 |
[์ฉ์ด] ํ๋กํ์ผ, ํ๋กํ์ผ๋ง (0) | 2024.07.26 |