๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿš€ Development/Android

[์•ˆ๋“œ๋กœ์ด๋“œ] Retrofit2 ๋ž€? (์‚ฌ์šฉ๋ฒ• ์˜ˆ์ œ)

by Jay Din 2023. 5. 16.
728x90
๋ฐ˜์‘ํ˜•

๋“ค์–ด๊ฐ€๊ธฐ์— ์•ž์„œ

Retrofit ์™ธ์—๋„ ํ†ต์‹ ์„ ํŽธํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ๋ช‡ ๊ฐœ ์žˆ์—ˆ๋‹ค.

HttpClient, Volley, Okhttp ๋“ฑ ํ•˜์ง€๋งŒ HttpClient, Volley๋Š” Deprecated ๋˜์—ˆ๊ณ ,

Okhttp์„ ์ด์šฉํ•˜์—ฌ ๋” ํŽธํ•˜๊ณ  ์‚ฌ์šฉํ•˜๊ธฐ ์‰ฝ๊ฒŒ ๋งŒ๋“  ๊ฒƒ์ด Retrofit์ด๋‹ค.

 

Okhttp๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ Retrofit์„ ๋งŒ๋“ค์—ˆ๊ธฐ ๋•Œ๋ฌธ์— Retrofit ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด OKhttp์˜ ๋ฉ”์„œ๋“œ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ฐ˜์‘ํ˜•

Retrofit2 ์ด๋ž€?

์•ˆ๋“œ๋กœ์ด๋“œ์—์„œ ๋„คํŠธ์›Œํฌ ํ†ต์‹ ์„ ์‰ฝ๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ๋Š” ์˜คํ”ˆ ์†Œ์Šค ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ž…๋‹ˆ๋‹ค.

Retrofit์€ HTTP API๋ฅผ ํ˜ธ์ถœํ•˜๊ธฐ ์œ„ํ•œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ •์˜ํ•˜๊ณ , ์ด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์‹ค์ œ๋กœ ๋„คํŠธ์›Œํฌ ํ˜ธ์ถœ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

  • API ํ†ต์‹ ์„ ์œ„ํ•ด ๊ตฌํ˜„๋œ OkHTTP์˜ HTTP ํ†ต์‹ ์„ ๊ฐ„ํŽธํ•˜๊ฒŒ ๋งŒ๋“ค์–ด์ฃผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋œปํ•œ๋‹ค.
  • Async Task๊ฐ€ ์—†์ด Background ์“ฐ๋ ˆ๋“œ๋ฅผ ์‹คํ–‰ํ•œ๋‹ค. ์ฆ‰, CallBack์„ ํ†ตํ•˜์—ฌ Main Thread์—์„œ UI๋ฅผ ์—…๋ฐ์ดํŠธํ•œ๋‹ค.
  • ๋™์ผ Squareup์‚ฌ์˜ OkHttp ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์ƒ์œ„ ๊ตฌํ˜„์ฒด์ด๋‹ค.

Retrofit 2 ์žฅ์ 

Retrofit2์˜ ์žฅ์ ์€ ํฌ๊ฒŒ 3๊ฐ€์ง€๋กœ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

์†๋„ / ํŽธ์˜์„ฑ / ๊ฐ€๋…์„ฑ

  • OkHTTP ์‚ฌ์šฉ ์‹œ์— AsyncTask๋ฅผ ํ†ตํ•ด ๋น„๋™๊ธฐ๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ์†๋„๊ฐ€ ๋Š๋ฆฐ ์ด์Šˆ๊ฐ€ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ Retrofit2์—์„œ๋Š” ์ž์ฒด์ ์œผ๋กœ ๋น„๋™๊ธฐ ์‹คํ–‰๊ณผ ์Šค๋ ˆ๋“œ ๊ด€๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•˜์—ฌ ์†๋„๊ฐ€ ๋น ๋ฅด๋‹ค. 
  • ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ์— ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋„˜๊ฒจ์ฃผ์–ด ์ž‘์—…๋Ÿ‰์ด ๊ฐ์†Œํ•œ๋‹ค.
  • Interface ๋‚ด์—์„œ ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ํ˜ธ์ถœ ํ•จ์ˆ˜๋ฅผ ๋ฏธ๋ฆฌ ์ง€์ •, ๊ตฌํ˜„ ์—†์ด ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœ๋งŒ ํ•ด์ฃผ๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ฝ”๋“œ๋ฅผ ์ฝ๊ธฐ ํŽธ๋ฆฌํ•˜๋‹ค. ๋˜ํ•œ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ†ตํ•œ ์ง๊ด€์  ์„ค๊ณ„๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.
  • Retrofit์€ ๋™๊ธฐ/๋น„๋™๊ธฐ๋ฅผ ๋ชจ๋‘ ์ง€์›ํ•œ๋‹ค.

Retrofit 2 ๊ตฌ์„ฑ์š”์†Œ

  • DTO (POJO) - 'Data transfer Object', 'Plain Old Java Object' ํ˜•ํƒœ์˜ ๋ชจ๋ธ(Model) / JSON ํƒ€์ž…๋ณ€ํ™˜์— ์‚ฌ์šฉ
    DTO (Data transfer Object) ๋ž€?
    ๊ณ„์ธต๊ฐ„ ๋ฐ์ดํ„ฐ ๊ตํ™˜์ด ์ด๋ฃจ์–ด ์งˆ ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๊ฐ์ฒด(Java Beans)์ด๊ธฐ ๋•Œ๋ฌธ์—, ํŠน๋ณ„ํ•œ ๋กœ์ง์„ ๊ฐ€์ง€์ง€ ์•Š๋Š” ์ˆœ์ˆ˜ํ•œ ๋ฐ์ดํ„ฐ ๊ฐ์ฒด์—ฌ์•ผ ํ•œ๋‹ค.
  • Interface - ์‚ฌ์šฉํ•  HTTP CRUD ๋™์ž‘(๋ฉ”์†Œ๋“œ)๋“ค์„ ์ •์˜ํ•ด ๋†“๋Š” ์ธํ„ฐํŽ˜์ด์Šค
  • Retrofit.Builder ํด๋ž˜์Šค - interface๋ฅผ ์‚ฌ์šฉํ•  ์ธ์Šคํ„ด์Šค, baseUrl(URL) / Converter(๋ณ€ํ™˜๊ธฐ) ์„ค์ •

Retrofit 2 ์˜ˆ์ œ

Retrofit2์„ ์ด์šฉํ•˜์—ฌ URL์„ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ์˜ˆ์ œ

 

AndroidManifest.xml

์ธํ„ฐ๋„ท ๊ถŒํ•œ ์„ค์ •

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

build.gradle

retrofit ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ถ”๊ฐ€ 

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

 

Post.kt

๊ฐ€์ ธ์˜ฌ ๋ฐ์ดํ„ฐ์˜ ๋ฐ์ดํ„ฐ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ ๋‹ค.

data class Post (
    val userId : Int,
    val id : Int,
    val title : String,
    val body : String
)

 

MyApi.kt

interface ์ •์˜, number ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด URL ๋’ค์— ์˜ค๋Š” ๋””ํ…Œ์ผํ•œ ์ฃผ์†Œ๋ฅผ ์žก์•„์ค€๋‹ค.

interface MyApi {

    @GET("posts/{number}")
    fun getPostNumber(
        @Path("number") number : Int
    ) : Call<Post>

}

RetrofitInstance.kt

retrofit ์ธ์Šคํ„ด์Šค๋ฅผ ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด์œผ๋กœ ๋งŒ๋“ค์–ด์ค€๋‹ค.

object RetrofitInstance {

    val BASE_URL = "https://jsonplaceholder.typicode.com/"

    val client = Retrofit
        .Builder()
        .baseUrl(BASE_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .build()

    fun getInstance() : Retrofit {
        return client
    }

}

MainActivity.kt

class MainActivity : AppCompatActivity() {
    
    class MainActivity : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)

            val api = RetrofitInstance.getInstance().create(MyApi::class.java)

            api.getPostNumber(2).enqueue(object : Callback<Post> {
                override fun onResponse(call: Call<Post>, response: Response<Post>) {
                    if (response.isSuccessful){
                        // ์ •์ƒ์ ์œผ๋กœ ํ†ต์‹  ์„ฑ๊ณตํ•œ ๊ฒฝ์šฐ
                        // URL์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.
                        Log.d("API2", response.body().toString())
                    }else{
                        // ํ†ต์‹ ์ด ์‹คํŒจํ•œ ๊ฒฝ์šฐ(์‘๋‹ต์ฝ”๋“œ 3xx, 4xx ๋“ฑ)
                        Log.d("API2", "ํ†ต์‹  ์‹คํŒจ")
                    }
                }
                override fun onFailure(call: Call<Post>, t: Throwable) {
                    // ํ†ต์‹  ์‹คํŒจ (์ธํ„ฐ๋„ท ๋Šํ‚ด, ์˜ˆ์™ธ ๋ฐœ์ƒ ๋“ฑ ์‹œ์Šคํ…œ์ ์ธ ์ด์œ )
                    Log.d("API2", "fail")
                }

            })

        }
    }
}
๋ฐ˜์‘ํ˜•

 

์‹คํ–‰ ํ™”๋ฉด

Post ํด๋ž˜์Šค์— ๋ฐ์ดํ„ฐ ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•ด ๋†“์€ ๊ฒƒ ์ฒ˜๋Ÿผ ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. 

 

 


์ฐธ๊ณ 

https://minchanyoun.tistory.com/44

 

728x90
๋ฐ˜์‘ํ˜•