Android with Kotlin

UpBit 실시간 코인 시세 확

JinTonix 2023. 3. 19. 21:36

retorfit를 이용 Volley를 이용해도 되는데 While문 에서 잘 죽음

 

Data Class

data class Upbit(
    val market: String,
    val trade_price: Double,
    val opening_price: Double,
    val high_price: Double,
    val low_price: Double,

    val change: String,
    val change_price: String,
    val change_rate: String,

    val signed_change_rate : Double,
    val signed_change_price : Double,
    val acc_trade_price_24h: Double
)

 

Interface

interface UpbitAPI {
    @GET("v1/ticker")
    fun getCoinInfo(
        @Query("markets")arg:String
    ): retrofit2.Call<List<Upbit>>
}

 

Builder

val UpbitRetrofit = Retrofit.Builder()
    .baseUrl("https://api.upbit.com/")
    .addConverterFactory(GsonConverterFactory.create())
    .build()

 

val UpbitService = UpbitRetrofit.create(UpbitAPI::class.java)

 

실제 사용

UpbitService.getCoinInfo(CoinList.get(i).cointName).enqueue(object :Callback<List<Upbit>>{
    override fun onResponse(call: retrofit2.Call<List<Upbit>>, response: Response<List<Upbit>>) {
        Log.d("Hey","Upbit Response : ${response.body()}")

        if(response.body()?.get(0)?.change.toString().equals("RISE")){
            textUpbitPrice.setTextColor(Color.RED)
            textUpbitRate.setTextColor(Color.RED)
        }
        else if(response.body()?.get(0)?.change.toString().equals("FALL")){
            textUpbitPrice.setTextColor(Color.BLUE)
            textUpbitRate.setTextColor(Color.BLUE)
        }
        else{
            textUpbitPrice.setTextColor(Color.BLACK)
            textUpbitRate.setTextColor(Color.BLACK)
        }

        textUpbitName.text = response.body()?.get(0)?.market.toString()

        CoinList.get(i).coinPirce = response.body()?.get(0)?.trade_price!!.toDouble()

        textUpbitPrice.text = CoinList.get(i).coinPirce.toString() + " ₩"
        FirmaPrice = CoinList.get(i).coinPirce

        textUpbitRate.text = decimal.format(((response.body()?.get(0)?.change_rate.toString()).toFloat() * 100)).toString() + "%" + "\n" + "${
            abs(response.body()!!.get(0).signed_change_price)}"

        textUpbitVol.text = decimalVol.format(((response.body()?.get(0)?.acc_trade_price_24h.toString()).toFloat()) /DIVIDE_VALUE).toString()

    }

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

'Android with Kotlin' 카테고리의 다른 글

CallBack 사용법  (0) 2023.03.19
파일 읽고 쓰기  (0) 2023.03.19
Gson 파일 읽고 쓰기 - Havah  (0) 2023.03.19
ByBit 실시간 거래 값 가져오기  (0) 2023.03.19
Single Tone  (0) 2023.03.01