๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“ฑ| Android/๐Ÿ“˜ | ๊ธฐ๋ก

[Android/Kotlin] retrofit2 ์ •๋ฆฌ

by immgga 2022. 5. 13.

์˜ค๋Š˜ ์˜ˆ์ œ๋Š” http์™€ ํ†ต์‹ ํ•˜๋Š” ๋ฐฉ๋ฒ•์ธ retrofit์— ๋Œ€ํ•ด ์ •๋ฆฌํ•ด๋ณด์•˜๋‹ค.

 

1. gradle ์ถ”๊ฐ€

implementation "com.squareup.retrofit2:retrofit:2.9.0"
implementation "com.squareup.retrofit2:converter-gson:2.9.0"

 

2. api service

api ๊ตฌํ˜„์€ ๋ฐ•์Šค์˜คํ”ผ์Šค ์˜ํ™” ์ˆœ์œ„๋ฅผ ์˜ˆ๋กœ ๋“ค๊ฒ ๋‹ค.

interface RetrofitInterface {
    @GET("http://kobis.or.kr/kobisopenapi/webservice/rest/boxoffice/searchDailyBoxOfficeList.json")
    fun getBoxOffice(
        @Query("key") key: String?,
        @Query("targetDt") targetDt: String?,
    ): Call<Result?>?
}

ํ•จ์ˆ˜ ์œ„์— GET, POST, DELETE, PUT, HEAD ์ค‘ ์•Œ๋งž๋Š” ๊ธฐ๋Šฅ์„ ์“ฐ๊ณ  ์•ˆ์— url์„ ์ž‘์„ฑํ•œ๋‹ค.

 

3. api call response ํด๋ž˜์Šค ์ž‘์„ฑ

๋ฐ›์•„์˜ฌ ๋ฐ์ดํ„ฐ๋“ค์„ ์„ ์ •ํ•ด data class๋ฅผ ๋งŒ๋“ ๋‹ค.

data class BoxOfficeResult (
    @SerializedName("boxofficeType")
    @Expose
    var boxofficeType: String,

    @SerializedName("showRange")
    @Expose
    var showRange: String,

    @SerializedName("dailyBoxOfficeList")
    @Expose
    var dailyBoxOfficeList: List<DailyBoxOfficeList>? = null
)
  • @SerializedName: JSON์œผ๋กœ serialize ๋  ๋•Œ ๋งค์นญ๋˜๋Š” ์ด๋ฆ„์„ ๋ช…์‹œํ•˜๋Š” ๋ชฉ์ ์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.
  • @Expose : object ์ค‘ ํ•ด๋‹น ๊ฐ’์ด null์ผ ๊ฒฝ์šฐ, json์œผ๋กœ ๋งŒ๋“ค ํ•„๋“œ๋ฅผ ์ž๋™ ์ƒ๋žต

 

4. retrofit builder

class RetrofitClient {
    private var instance: RetrofitClient? = null
    private var retrofitInterface: RetrofitInterface
    private var baseUrl: String = "http://www.kobis.or.kr"

    init {
        val retrofit = Retrofit.Builder()
            .baseUrl(baseUrl)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
        retrofitInterface = retrofit.create(RetrofitInterface::class.java)
    }

    fun getInstance(): RetrofitClient? {
        if (instance == null) {
            instance = RetrofitClient()
        }
        return instance
    }

    fun getRetrofitInterface(): RetrofitInterface {
        return retrofitInterface
    }
}

 

 

5. ๊ตฌํ˜„

package com.example.retrofitapplication

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.retrofitapplication.class1.RetrofitClient
import com.example.retrofitapplication.class1.RetrofitInterface
import com.example.retrofitapplication.class1.data.BoxOfficeResult
import com.example.retrofitapplication.class1.recyclerview.MovieAdapter
import retrofit2.Call
import retrofit2.Response
import java.text.SimpleDateFormat
import java.util.*
import retrofit2.Callback
import com.example.retrofitapplication.class1.data.Result


class MainActivity : AppCompatActivity() {

    private lateinit var recyclerView: RecyclerView
    private lateinit var mAdapter: MovieAdapter

    private lateinit var retrofitClient: RetrofitClient
    private lateinit var retrofitInterface: RetrofitInterface

    private val API_KEY: String = "32959eb64a93e024aaf4bb540b00e85c"    // Api ํ‚ค
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        recyclerView = findViewById(R.id.recyclerView)
        recyclerView.layoutManager = LinearLayoutManager(applicationContext, LinearLayoutManager.VERTICAL, false)

        // ์ดˆ๊ธฐํ™”
        retrofitClient = RetrofitClient().getInstance()!!
        retrofitInterface = RetrofitClient().getRetrofitInterface()

        // ํ•˜๋ฃจ ์ „ ๋‚ ์งœ ๊ตฌํ•˜๊ธฐ
        val day: Calendar = Calendar.getInstance()
        day.add(Calendar.DATE, -1)
        val time: String =SimpleDateFormat("yyyyMMdd").format(day.time).toString()

        retrofitInterface.getBoxOffice(API_KEY, time)!!.enqueue(object : Callback<Result?> {

            override fun onResponse(call: Call<Result?>, response: Response<Result?>) {
                val result: Result? = response.body()
                Log.d("retrofit", "onResponse result: $result")
                val boxOfficeResult: BoxOfficeResult = result!!.boxOfficeResult
                Log.d("retrofit", "Data fetch success")
                mAdapter = MovieAdapter(boxOfficeResult.dailyBoxOfficeList!!)
                recyclerView.adapter = mAdapter
            }

            override fun onFailure(call: Call<Result?>?, t: Throwable) {
                Log.d("retrofit", t.message!!)
            }
        })

    }

 


retrofit์€ ๊ฐœ๋ฐœํ• ๋•Œ ์ž์ฃผ ์“ฐ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ผญ ์•Œ์•„๋‘๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

728x90