๐Ÿ”ฅTIL๐Ÿ”ฅ์ŠคํŒŒ๋ฅดํƒ€ | Retrofit ์‚ฌ์šฉํ•˜๊ธฐ - ๋ฏธ์„ธ๋จผ์ง€์•ฑ

hyihyiยท2024๋…„ 1์›” 26์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
41/70
post-thumbnail

Room : DB๋ฅผ ํŽธํ•˜๊ฒŒ ์“ฐ๊ธฐ ์œ„ํ•œ ๊ฒƒ
Retrofit : HTTP ํ†ต์‹ ์„ ํŽธํ•˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•œ ๊ฒƒ

1. ๊ถŒํ•œ ์„ค์ •/๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ถ”๊ฐ€

์ธํ„ฐ๋„ท ๊ถŒํ•œ ์ถ”๊ฐ€
์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ํ‰๋ฌธ(HTTP) ๋„คํŠธ์›Œํฌ ํŠธ๋ž˜ํ”ฝ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค์ •

AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET"/>


android:usesCleartextTraffic="true"

build.gradle

implementation("com.google.code.gson:gson:2.10.1")
implementation("com.squareup.retrofit2:retrofit:2.9.0")
implementation("com.squareup.retrofit2:converter-gson:2.9.0")
implementation("com.squareup.okhttp3:okhttp:4.10.0")
implementation("com.squareup.okhttp3:logging-interceptor:4.10.0")

2. DTO ์ž‘์„ฑ

DustDTO ์ž‘์„ฑ

์„œ๋ฒ„์—์„œ ๋ณด๋‚ด์ฃผ๋Š” JSON ๊ฐ’์˜ ๋ฐ์ดํ„ฐ ํด๋ž˜์Šค๋ฅผ ์‰ฝ๊ฒŒ ์ž‘์„ฑํ•˜๊ธฐ ์œ„ํ•ด ํ”Œ๋Ÿฌ๊ทธ์ธ ์‚ฌ์šฉ
File>Settings>Plugins>"JSON To Kotlin Class install"ํ•˜๊ธฐ

res>New>Kotlin data class File from JSON ๋ˆ„๋ฅด๊ธฐ

JSON ํŒŒ์ผ์„ ์ž…๋ ฅํ•˜๊ณ  (format์„ ๋ˆ„๋ฅด๊ณ ) Class Name์„ ์ž…๋ ฅํ•˜๋ฉด ์•„๋ž˜์ฒ˜๋Ÿผ Data Class๋“ค์ด ๋งŒ๋“ค์–ด์ง„๋‹ค.

Dust

data class Dust(
    val response: DustResponse
)

DustResponse

data class DustResponse(
    @SerializedName("body")
    val dustBody: DustBody,
    @SerializedName("header")
    val dustHeader: DustHeader
)

DustBody

data class DustBody(
    val totalCount: Int,
    @SerializedName("items")
    val dustItems: MutableList<DustItem>?,
    val pageNo: Int,
    val numOfRows: Int
)

3. API Interface ์ •์˜

์–ด๋Š ์ฃผ์†Œ๋กœ ์–ด๋–ค ์š”์ฒญ ๋ณ€์ˆ˜๋ฅผ ์ฃผ๊ณ  ์–ด๋–ค ๊ฐ’์„ ๋ฐ›๋Š”์ง€
ํŒŒ์‹ฑ์€ Gson์ด ํ•ด์คŒ

interface NetworkInterface {
    @GET("getCtprvnRltmMesureDnsty") //์‹œ๋„๋ณ„ ์‹ค์‹œ๊ฐ„ ์ธก์ •์ •๋ณด ์กฐํšŒ ์ฃผ์†Œ
    suspend fun getDust(@QueryMap param: HashMap<String, String>): Dust //HashMap์— ์š”์ฒญ๋ณ€์ˆ˜๋“ค์ด ๋“ค์–ด๊ฐ ex)์„œ๋น„์Šคํ‚ค, ํŽ˜์ด์ง€ ๋ฒˆํ˜ธ, ์‹œ๋„๋ช… ...
}

4. Retrofit ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ

Retrofit์€ ํ•ญ์ƒ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

//Retrofit Client
object NetWorkClient {
    private const val DUST_BASE_URL = "http://apis.data.go.kr/B552584/ArpltnInforInqireSvc/"

    private fun createOkHttpClient(): OkHttpClient {
        val intercepter = HttpLoggingInterceptor()

        //ํ†ต์‹ ์ด ์ž˜ ์•ˆ ๋  ๋•Œ ๋””๋ฒ„๊น…์„ ์œ„ํ•œ ์šฉ๋„๋กœ ๋„ฃ์€ ๊ฒƒ
        if (BuildConfig.DEBUG){
            intercepter.level = HttpLoggingInterceptor.Level.BODY
        } else{
            intercepter.level = HttpLoggingInterceptor.Level.NONE
        }

        return OkHttpClient.Builder()
            .connectTimeout(20,TimeUnit.SECONDS)
            .readTimeout(20, TimeUnit.SECONDS)
            .writeTimeout(20, TimeUnit.SECONDS)
            .addNetworkInterceptor(intercepter)
            .build()
    }

    private val dustRetrofit = Retrofit.Builder()
        .baseUrl(DUST_BASE_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .client(createOkHttpClient())
        .build()

    val dustNetWork: NetworkInterface = dustRetrofit.create(NetworkInterface::class.java)
}

5. ๋ฐ์ดํ„ฐ ๋ฐ›์•„์˜ค๊ธฐ

5.1 API ํ‚ค

5.2 ์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ ์ƒ์„ฑ

ํ•ญ๋ชฉ๋ช…์„ ๊ทธ๋Œ€๋กœ ์ ์–ด์•ผ ํ•œ๋‹ค.

private fun setUpDustParameter(sido: String): HashMap<String, String> {
        val authKey =
            "nrOGw0QURU.........."

        return hashMapOf(
            "serviceKey" to authKey,
            "returnType" to "json",
            "numOfRows" to "100",
            "pageNo" to "1",
            "sidoName" to sido,
            "ver" to "1.0"
        )
    }

5.3 ๋„คํŠธ์›Œํฌ ํ†ต์‹ 

๋ฐ์ดํ„ฐ ํ†ต์‹ ์„ ํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ณ„๋„์˜ ์Šค๋ ˆ๋“œ ์•ˆ์—์„œ ์‹คํ–‰

    private fun communicateNetWork(param: HashMap<String, String>) = lifecycleScope.launch {
        val responseData = NetWorkClient.dustNetWork.getDust(param)

        items = responseData.response.dustBody.dustItems!!

        val goo = ArrayList<String>()
        items.forEach {
            Log.d("add Item: ", it.stationName)
            goo.add(it.stationName)
        }

        runOnUiThread {
            binding.spinnerViewGoo.setItems(goo)
        }
    }
profile
๋‚ด๊ฐ€ ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ฒŒ ์“ฐ๋Š” ๋ธ”๋กœ๊ทธ

0๊ฐœ์˜ ๋Œ“๊ธ€