์ค๋ ์์ ๋ 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
'๐ฑ| Android > ๐ | ๊ธฐ๋ก' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Android, Kotlin] Android์์ Apollo๋ฅผ ์ด์ฉํ GraphQL์ฌ์ฉํ๊ธฐ (0) | 2022.10.23 |
---|---|
[Android, Kotlin] editText์ toggle icon์ด ๋ฐ๋๋ก ๋์์ ๋ ํด๊ฒฐ ๋ฐฉ๋ฒ (2) | 2022.08.31 |
[Android] ํ๋ฉด ์ด๋ ๋ฐฉ๋ฒ ์ ๋ฆฌ (0) | 2022.04.12 |
[Android Kotlin] radio button ์์ (0) | 2022.04.11 |
[Android] Clean Architecture ๊ธฐ๋ก (0) | 2022.04.01 |