[Android] Retrofit2

강승구·2022년 12월 23일
0

Retrofit은 안드로이드와 서버간의 REST API 통신을 도와주는 라이브러리로, okHTTP에 기반을 두고 있다.
안드로이드에서 API서버와 통신하기 위한 방법으로 HttpUrlConnection, Volley, OkHttp, Retrofit2 등이 존재한다. 그중에서 Retrofit2는 높은 성능과 가독성으로 현재 가장 인기가 많은 통신 라이브러리 중 하나이다.

Retrofit의 장점

1. 높은 성능

HTTP 통신을 지원하는 다른 라이브러리들과 비교했을 때 높은 성능을 보여준다.
img

2. 뛰어난 가독성

Annotation으로 HTTP 메소드를 정의함으로서 코드의 구현이 쉬워지며 개발자들은 행위를 손쉽게 알아볼 수 있게 되어 직관적으로 코드를 설계할 수 있게 된다.

3. 쉬운 유지보수

Retrofit은 서버 연동 시 주로 주고받는 데이터인 JSON, XML을 자동을 파싱해주는 Converter 연동을 지원해주기 때문에 개발자 입장에서는 유지보수가 매우 편리하다.


사용법

1. dependency 추가

implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.6.0'

2. internet permission 부여

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

3. Retrofit Builder 생성

object 키워드를 이용하여 싱글톤으로 생성해준다.

import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

object RetrofitBuilder{
    var api: RetrofitService
    init {
        val retrofit = Retrofit.Builder()
            .baseUrl("https://api.github.com/")
            .addConverterFactory(GsonConverterFactory.create())
            .build()

        api = retrofit.create(RetrofitService::class.java)
    }
}

3. DTO 클래스 생성

Data Transfer Object의 약자로 JSON Object 파싱을 위해 사용하는 클래스이다.

import com.google.gson.annotations.SerializedName

data class UserInfo(
    @SerializedName("login")
    val id: String,
    val name: String,
    val created_at: String,
    val updated_at: String,
    val followers: Int,
    val following: Int
)

4. API 인터페이스 생성

import retrofit2.Call
import retrofit2.http.GET
import retrofit2.http.Path

interface RetrofitService{
    @GET("users/{userId}")
    fun getUserInfo(@Path("userId") userId: String): Call<UserInfo>
}

5. 요청 보내기

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response


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

        val button: Button = findViewById(R.id.button)
        var inputId: EditText = findViewById(R.id.input_id)
        var name: TextView = findViewById(R.id.name_text)
        var followers: TextView = findViewById(R.id.followers_text)
        var following: TextView = findViewById(R.id.following_text)
        var createDate: TextView = findViewById(R.id.createDate_text)
        var updateDate : TextView = findViewById(R.id.updateDate_text)

        button.setOnClickListener {
            var userId: String = inputId.text.toString()
            RetrofitBuilder.api.getUserInfo(userId).enqueue(object : Callback<UserInfo> {
                override fun onResponse(call: Call<UserInfo>, response: Response<UserInfo>) {
                    var userInfo = response.body()
                    if (userInfo != null){
                        name.text = "이름 : ${userInfo.name}"
                        following.text = "Following : ${userInfo.following.toString()}"
                        followers.text = "Followers : ${userInfo.followers.toString()}"
                        createDate.text = "계정 만든 날짜 : ${textSlicing(userInfo.created_at)}"
                        updateDate.text = "최근 업데이트한 날짜 : ${textSlicing(userInfo.updated_at)}"
                    }else{
                        Toast.makeText(this@MainActivity, "ID 오류", Toast.LENGTH_LONG).show()
                    }
                }

                override fun onFailure(call: Call<UserInfo>, t: Throwable) {
                    Toast.makeText(this@MainActivity, "fail", Toast.LENGTH_LONG).show()
                }
            })
        }
    }

    fun textSlicing(text: String): String = (text.replace("Z", " ")).replace("T", " ")
}

전체 코드

https://github.com/kang9366/Android_Study/tree/main/AndroidEssential_Retrofit

profile
강승구

0개의 댓글