[TIL] ๐ผ24/04/11๐ผ
Moshi & Retrofit2
- ์ค๋์ API ํ์ฉ ๋ก์ง์์ Gson -> Moshi ์ปจ๋ฒํ
์ ํด๋ณด์๋ค.
๐์ฐธ๊ณ ์๋ฃ
val type = Types.newParameterizedType(List::class.java, String::class.java)
val adapter = moshi.adapter<List<String>>(type)
๋ฆฌํฉํ ๋ง ์
private val tourRetrofit = Retrofit.Builder()
.baseUrl(TOUR_BASE_URL)
.addConverterFactory(
GsonConverterFactory.create(
GsonBuilder()
.setLenient()
.create()
)
)
.client(createOkHttpClient())
.build()
๋ฆฌํฉํ ๋ง ํ
- build.gradle(app)์ dependency ์ถ๊ฐ
implementation("com.squareup.retrofit2:converter-moshi:2.9.0")
- data class์
@SerializedName("")
์ ๋
ธํ
์ด์
@Json(name="")
์ผ๋ก ๋ณ๊ฒฝ
private val moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.build()
private val tourRetrofit = Retrofit.Builder()
.baseUrl(TOUR_BASE_URL)
.addConverterFactory(
MoshiConverterFactory.create(moshi).asLenient()
)
.client(createOkHttpClient())
.build()