Android with Kotlin

ByBit 실시간 거래 값 가져오기

JinTonix 2023. 3. 19. 21:33

Retrofit를 이용하였다. 

 

Data Class

data class Bybit(

  val retCode: Int,
  val retMsg: String,
  val result: TickerResult

)

data class TickerResult(
  val t: Long,
  val s: String,
  val bp: String,
  val ap: String,
  val lp: String,
  val o: String,
  val h: String,
  val l: String,
  val v: String,
  val qv: String
)

 

Interface

interface BybitAPI {
    @GET("spot/v3/public/quote/ticker/24hr")
    fun getCoinInfo(
        @Query("symbol")arg:String
    ): retrofit2.Call<Bybit>
}

 

Builder

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

 

val BybitService = BybitRetrofit.create(BybitAPI::class.java)

 

실제 사용

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

        var temp = (response.body()?.result?.o)?.toFloat()
            ?.minus((response.body()?.result?.lp)?.toFloat()!!)

        if(temp!! < 0){
            textBybitPrice.setTextColor(Color.RED)
            textBybitRate.setTextColor(Color.RED)
        }
        else if(temp!! > 0){
            textBybitPrice.setTextColor(Color.BLUE)
            textBybitRate.setTextColor(Color.BLUE)
        }
        else{
            textBybitPrice.setTextColor(Color.BLACK)
            textBybitRate.setTextColor(Color.BLACK)
        }

        textBybitName.text  = response.body()?.result?.s

        CoinList.get(i).coinPirce = response.body()!!.result.lp.toDouble()
        textBybitPrice.text = CoinList.get(i).coinPirce.toString() + "\n" + "${decimal.format(CoinList.get(i).coinPirce * CurrentWON)} ₩"
        ByBitPrice = CoinList.get(i).coinPirce * CurrentWON

        textBybitRate.text = (decimal.format(abs(
            (temp?.div((response.body()?.result?.o)?.toFloat()!!))?.times(100)!!
        )).toString()) + "%" +"\n" + "${decimalBybit.format(abs(temp.toFloat()))}"

        textBybitVol.text = decimalVol.format(response.body()?.result?.qv?.toFloat()).toString()
    }

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

 

 

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

CallBack 사용법  (0) 2023.03.19
파일 읽고 쓰기  (0) 2023.03.19
Gson 파일 읽고 쓰기 - Havah  (0) 2023.03.19
UpBit 실시간 코인 시세 확  (0) 2023.03.19
Single Tone  (0) 2023.03.01