기존의 Retrofit 호출 코드
package com.example.data.api
import android.graphics.Color
import android.util.Log
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
class Retrofit(){
private lateinit var apiService: ApiService
fun call롯데월드(){
val retrofit = Retrofit.Builder()
.baseUrl("http://115.21.135.45:8000/")
.addConverterFactory(GsonConverterFactory.create())
.build()
apiService = retrofit.create(com.example.data.api.ApiService::class.java)
val call = apiService.getData()
call.enqueue(object : Callback<ApiResponse> {
override fun onResponse(call: Call<ApiResponse>, response: Response<ApiResponse>) {
if (response.isSuccessful) {
val apiResponse = response.body()
if(apiResponse != null){
val area = apiResponse.롯데월드
val congestionLevel = area.congestionLevel
val datetime = area.datetime
// ... custom
}
} else{
Log.e("API Error", "Request failed with code: ${response.code()}")
}
}
override fun onFailure(call: Call<com.example.data.api.ApiResponse>, t: Throwable) {
// API 요청 실패 처리
t.printStackTrace()
}
})
}
....(지역 5개 더있음)
1) 리플랙션은 코드가 실행 중인 프로그램의 메타 데이터(즉, 그 자체의 구조와 특성)에 액세스하고 수정할 수 있게 함.
2) 직접 매핑
class Retrofit(){
private val retrofit: Retrofit = Retrofit.Builder()
.baseUrl("http://115.21.135.45:8000/")
.addConverterFactory(GsonConverterFactory.create())
.build()
private val apiService: ApiService = retrofit.create(com.example.data.api.ApiService::class.java)
fun callApi(areaName: String) {
val call = apiService.getData()
call.enqueue(object : Callback<ApiResponse> {
override fun onResponse(call: Call<ApiResponse>, response: Response<ApiResponse>) {
if (response.isSuccessful) {
val apiResponse = response.body()
if(apiResponse != null){
val area = apiResponse.getClassField(areaName) // Reflectively get the field
val congestionLevel = area.congestionLevel
val datetime = area.datetime
// ... custome
}
} else {
Log.e("API Error", "Request failed with code: ${response.code()}")
}
}
override fun onFailure(call: Call<ApiResponse>, t: Throwable) {
// API 요청 실패 처리
t.printStackTrace()
}
})
}
// This is a helper function to get the value of the class field using reflection
private fun ApiResponse.getClassField(fieldName: String): Hotspot {
val field = this::class.java.getDeclaredField(fieldName)
return field.get(this) as Hotspot
}
}
// Retrofit 클래스 이름 변경
class ApiServiceManager(){
private val retrofit: Retrofit = Retrofit.Builder()
// ... 기존 코드
.build()
private val apiService: ApiService = retrofit.create(com.example.data.api.ApiService::class.java)
fun callApi(areaName: String) {
val call = apiService.getData()
call.enqueue(object : Callback<ApiResponse> {
override fun onResponse(call: Call<ApiResponse>, response: Response<ApiResponse>) {
// ... 기존 코드
}
// ... 기존 코드
})
}
// 리플렉션 대신 사용할 수 있는 매핑 로직
private fun ApiResponse.getAreaByName(areaName: String): Hotspot? {
return when(areaName) {
"롯데월드" -> 롯데월드
"방이동먹자골목" -> 방이동먹자골목
"에비뉴엘월드타워점" -> 에비뉴엘월드타워점
"롯데월드몰" -> 롯데월드몰
"올림픽공원" -> 올림픽공원
else -> null
}
}
}