Android with Kotlin

환율 정보 받아오기

JinTonix 2023. 3. 19. 22:18
class ExChange(
    val basePrice: String
)

 

Interface

interface ExChangeAPI{
    @GET("v1/forex/recent")
    fun getExChangeInfo(
        @Query("codes")arg:String
    ): retrofit2.Call<List<ExChange>>
}

 

Builder

val ExChangeRetrofit = Retrofit.Builder()
    .baseUrl("https://quotation-api-cdn.dunamu.com/")
    .addConverterFactory(GsonConverterFactory.create())
    .build()

 

val ExChangeService = ExChangeRetrofit.create(ExChangeAPI::class.java)

 

Callback

ExChangeService.getExChangeInfo(value).enqueue(object :Callback<List<ExChange>>{
    override fun onResponse(
        call: retrofit2.Call<List<ExChange>>,
        response: Response<List<ExChange>>
    ) {
        CurrentWON = (response.body()!!.get(0).basePrice).toDouble()
    }

    override fun onFailure(call: retrofit2.Call<List<ExChange>>, t: Throwable) {
        Log.d("Hey","ExChangeService onFailure")
    }
})

 

사용

SetExChange("FRX.KRWUSD")