Android

[Android]Retrofit2์™€ REST API ํ†ต์‹  1 ( Kotlin )

Forest Yun 2021. 6. 18. 18:07
728x90

Retrofit

: Android ์™€ Java ํ™˜๊ฒฝ์—์„œ HTTP API๋ฅผ ์‰ฝ๊ณ  ์•ˆ์ „ํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๋‹ค. REST API ํ†ต์‹ ์„ ์œ„ํ•ด ๊ตฌํ˜„๋˜์—ˆ๋‹ค.

โ€‹

โ€‹

REST API ์„ค๋ช…

https://blog.naver.com/88yhtserof/222208916350

[Android] ์•ˆ๋“œ๋กœ์ด๋“œ REST API

โ€‹HTTP (Hypertext Transfer Protocol: ํ•˜์ดํผ๋ณธ๋ฌธ ์ „์†ก๊ทœ์•ฝ) : ์ธํ„ฐ๋„ท์—์„œ, ์›น ์„œ๋ฒ„์™€ ์‚ฌ์šฉ์ž์˜ ์ธ...

blog.naver.com

โ€‹

โ€‹

โ€‹


โ€‹

โ€‹

โ€‹

โ€‹

Retrofit2 ๋ฅผ ์ด์šฉํ•œ JSON ๋ฐ์ดํ„ฐ HTTP ํ†ต์‹ 

โ€‹

Retrofit2 ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด Gradle ์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ถ”๊ฐ€ํ•ด์ค€๋‹ค.โ€‹

dependencies{ implementation 'com.squareup.retrofit2:retrofit:(๋งˆ์ง€๋ง‰ ๋ฒ„์ „)' }

2021.01.21 ๊ธฐ์ค€ 2.9.0 ๋ฒ„์ „์ด ๊ฐ€๋Šฅ ๋งˆ์ง€๋ง‰ ๋ฒ„์ „์ด๋‹ค. ๋ฒ„์ „ ํ™•์ธ: https://github.com/square/retrofit

โ€‹

โ€‹

โ€‹

โ€‹

โ€‹

Retrofit์€ HTTP API ๋ฅผ ์ธํ„ฐํŽ˜์ด์Šค ํ˜•ํƒœ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ,

์ฝ”ํ‹€๋ฆฐ ์‚ฌ์šฉ ์‹œ ์•„๋ž˜์˜ ์–‘์‹์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

public interface RetrofitInterface { @HTTP์š”์ฒญ๋ฉ”์†Œ๋“œ("/๋””๋ ‰ํ† ๋ฆฌ/ํŒŒ์ผ๋ช…") fun ํ•จ์ˆ˜๋ช…( @Annotation("๋ณ€์ˆ˜") ๋ณ€์ˆ˜: ๋ณ€์ˆ˜ ์ž๋ฃŒํ˜• ): Call<Response์ž๋ฃŒํ˜•,์ฆ‰ ์ฃผ๊ณ  ๋ฐ›์„ ๊ฐ์ฒด)> }

โ€‹

์ฃผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๊ตฌ์„ฑ์š”์†Œ๋กœ REST API ํ†ต์‹ ์— ํ•„์š”ํ•œ Annotation์ด ์žˆ๋‹ค.

Annotation ์„ค๋ช… ์˜ˆ์‹œ
@GET โ€‹
HTTPํ†ต์‹ ์— ์‚ฌ์šฉ๋˜๋Š” method ์ค‘ GET ์š”์ฒญ์„ ๋ช…์‹œํ•œ๋‹ค.
โ€‹
โ€‹
@Path โ€‹
URL ๋ถ€๋ถ„ ์ค‘ ์ผ๋ถ€์˜ ๊ฒฝ๋กœ๊ฐ€ ํ•„์š”์— ๋”ฐ๋ผ ๋™์ ์œผ๋กœ ๋ฐ”์ธ๋”ฉ ๋˜์–ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ์— ์‚ฌ์šฉํ•˜๋ฉฐ
๋ฌธ์ž์—ด์„ ์ •์˜ ํ•ด๋‘๋ฉด ํ•ด๋‹น ๋ฌธ์ž์—ด์„ URL ๋ถ€๋ถ„ ์ผ๋ถ€์— ๋ฐ”์ธ๋”ฉํ•˜์—ฌ ๋™์ ์œผ๋กœ URL์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ค€๋‹ค.
โ€‹
//๊ฒ€์ƒ‰
โ€‹
@GET("/search/{word}")
fun requestSearch(
@Path("word") word:String
): Call<Response์ž๋ฃŒํ˜•>
โ€‹
@Query

GET ๋ฐฉ์‹์—์„œ๋งŒ ๊ฐ€๋Šฅํ•œ Annotation.
์˜ˆ์‹œ์˜ ๊ฒฝ์šฐ, id์— "2021" ๊ฐ’์ด ๋“ค์–ด๊ฐ€๊ฒŒ ๋œ๋‹ค๋ฉด
http://๋„๋ฉ”์ธ/post?postId=2021
์„ ํ˜ธ์ถœํ•˜๊ฒŒ ๋œ๋‹ค.
โ€‹
//๊ฒŒ์‹œ๊ธ€ ์กฐํšŒ
โ€‹
@GET("/post")
fun requestPost(
@Query("postId") id String
): Call<Response์ž๋ฃŒํ˜•>
@Field โ€‹
์ฃผ๋กœ POST ๋ฐฉ์‹์˜ ํ†ต์‹ ์„ ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” Annotation(Patch ๋ฐฉ์‹์—์„œ๋„ ์‚ฌ์šฉ, GET์—์„œ๋Š” ์‚ฌ์šฉ๋ถˆ๊ฐ€)
Field ํ˜•์‹์„ ์‚ฌ์šฉํ•  ๋•Œ์—๋Š” Form์ด Encoding ๋˜์–ด์•ผํ•œ๋‹ค.
๋”ฐ๋ผ์„œ @ FormUrlEncoding Annotation๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•œ๋‹ค.
โ€‹
//๋น„๋ฐ€๋ฒˆํ˜ธ ์„ค์ •
โ€‹
@FormUrlEncoding
@POST("/user/{userId}")
fun requestPassword(
@Path("userId") id:String,
@Field("password") password:String
): Call<Response์ž๋ฃŒํ˜•>
@FieldMap โ€‹
Field ํ˜•์‹์„ ํ†ตํ•ด ๋„˜๊ฒจ์ฃผ๋Š” ๊ฐ’๋“ค์ด ์—ฌ๋Ÿฌ๊ฐœ์ผ ๋•Œ @FieldMap Annotation ์„ ์‚ฌ์šฉํ•œ๋‹ค. Retrofit์—์„œ๋Š” HashMap์„ ๊ถŒ์žฅํ•œ๋‹ค.
โ€‹
โ€‹
@Part โ€‹
Mutipart ์š”์ฒญ ์‹œ ์‚ฌ์šฉํ•˜๋ฉฐ @Multipart Annotation๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•œ๋‹ค.
(๋‚˜์ค‘์— ๋‹ค์‹œ ์•Œ์•„๋ณด๊ธฐ)
โ€‹
โ€‹
@Headers/
@Header
โ€‹
์ •์  ํ—ค๋”๋“ค์€ @Headers๋ฅผ ํ†ตํ•ด ๋ช…์‹œํ•  ์ˆ˜ ์žˆ๋‹ค.
+๊ฐ™์€ ์ด๋ฆ„์ด์–ด๋„ ๊ฐ’์„ ๋ฎ์–ด์”Œ์šฐ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๋™์ผํ•œ ์ด๋ฆ„์˜ ํ—ค๋”๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฒŒ ๋˜๋ฉด ๋™์ผํ•œ ์ด๋ฆ„์œผ๋กœ ์š”์ฒญ์— ์ถ”๊ฐ€๋œ๋‹ค.
/
๋™์  ํ—ค๋”๋Š” @Header ๋ฅผ ํ†ตํ•ด ๋ช…์‹œํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฐ˜๋“œ์‹œ @Header ์— ๋Œ€์‘ํ•˜๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ œ๊ณตํ•ด์•ผํ•œ๋‹ค.
โ€‹
//์ •์  ํ—ค๋”
@Headers(
"~:~" )
โ€‹
//๋™์  ํ—ค๋”
fun ํ•จ์ˆ˜๋ช…(
@Headers("๋ณ€์ˆ˜") ๋ณ€์ˆ˜: ๋ณ€์ˆ˜์ž๋ฃŒํ˜•
)
@Body ์ „์†กํ•˜๋Š” ๊ฐ’์ด ํŠน์ • JSON ํ˜•์‹์ผ ๋•Œ ๋ฐ”๋กœ ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด ๊ฐ’์„ ๋„˜๊ฒจ์ฃผ๋Š” ํ˜•์‹ //user ๋ฐ์ดํ„ฐ ํด๋ž˜์Šค๊ฐ€ ์žˆ์„ ๋•Œ
data class User(
val name:String,
val nickname:String
)
โ€‹
//user ์ •๋ณด ์ˆ˜์ •
@PUT("/user/{userId}")
fun requestUserUpdate(
@Path("userId") id:String,
@Body body: User
): Call<Response์ž๋ฃŒํ˜•>

โ€‹

โ€‹


โ€‹

โ€‹

โ€‹

โ€‹

ํ—ค๋”๋ฅผ ๋ชจ๋“  ์š”์ฒญ๋งˆ๋‹ค ์ถ”๊ฐ€ํ•ด์•ผํ•œ๋‹ค๋ฉด, OkHttp interceptor ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ข‹๋‹ค.

โ€‹

OkHttp

: HTTP๋ฅผ ๊ฐ„ํŽธํ•˜๊ณ  ํšจ์œจ์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ๋Š” Client

interceptor:ํŠน์ • Url ์š”์ฒญ์‹œ Controller๋กœ ๊ฐ€๋Š” ์š”์ฒญ์„ ๊ฐ€๋กœ์ฑˆ๋‹ค.

โ€‹

โ€‹

OkHttp ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด Gradle ์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ถ”๊ฐ€ํ•ด์ค€๋‹ค.

๋ฒ„์ „ ํ™•์ธ: https://github.com/square/okhttp

dependencies{ implementation 'com.squareup.okhttp3:okhttp:(๋งˆ์ง€๋ง‰ ๋ฒ„์ „)' }

โ€‹

 

ํ†ต์‹ 

Retrofit ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•ด API์ฃผ์†Œ๊ฐ€ ์—ฐ๊ฒฐ๋˜๋„๋ก retrifit ๊ฐ์ฒด(์ธ์Šคํ„ด์Šค)๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

object RequestToServer { private const val EveryHeader="๋ชจ๋“ ํ—ค๋”" //๋กœ๊ทธ ๊ธฐ๋ก์— ์‚ฌ์šฉํ•  HttpLoggingInterceptor val httpLoggingInterceptor =HttpLoggingInterceptor() .setLevel(HttpLoggingInterceptor.Level.BODY) //๊ณ ์ • ํ—ค๋” ๊ฐ’์— ์‚ฌ์šฉํ•  Interceptor val headerInterceptor = Interceptor{ //Interceptor ์ธํ„ฐํŽ˜์ด์Šค val request = it.request() .newBuilder() .addHeader("EveryHearder", EveryHeader) //ํ—ค๋” ์ถ”๊ฐ€ .build() return@Interceptor it.proceed(request) //request๊ฐ€ ์‹คํ–‰ํ•  ์—ฐ๊ฒฐ ๋ฐ˜ํ™˜ } //client val client: OkHttpClient=OkHttpClient.Builder() .addInterceptor(headerInterceptor) //Interceptor ์ถ”๊ฐ€ .build() //Retrofit ๋นŒ๋“œ var retrofit: Retrofit = Retrofit.Builder() .baseUrl(์ฃผ์†Œ) .client(client) .addConverterFactory(GsonConverterFactory.create()) .build() val api:RetrofitInterface = retrofit.create(RetrofitInterface::class.java) }

addConverterFactory(GsonConverterFactory.create()) : ๊ฐ์ฒด์™€ JSON์˜ ์ž๋™์œผ๋กœ ๋ณ€ํ™”์‹œ์ผœ์ค€๋‹ค.

โ€‹

โ€‹

โ€‹

ํ˜ธ์ถœ์€ Retrofit2์™€ REST API ํ†ต์‹  2์—์„œ..

โ€‹

โ€‹

โ€‹


โ€‹

โ€‹

+

JSON โ€‹JavaScript Object Notation

: ์†์„ฑ-๊ฐ’ ์Œ ๋˜๋Š” ํ‚ค-๊ฐ’ ์Œ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฐ์ดํ„ฐ ์˜ค๋ธŒ์ ํŠธ๋ฅผ ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด ์ธ๊ฐ„์ด ์ฝ์„ ์ˆ˜ ์žˆ๋Š” ํ…์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฐœ๋ฐฉํ˜• ํ‘œ์ค€ ํฌ๋งท์ด๋‹ค. JSON์€ ์„œ๋ฒ„ํ†ต์‹ ์— ์‚ฌ์šฉ๋œ๋‹ค.

์ž๋ฃŒํ˜•์œผ๋กœ๋Š” ๊ธฐ๋ณธ์ž๋ฃŒํ˜•, ๋ฌธ์ž์—ด, ๋ฐฐ์—ด, ๊ฐ์ฒด๊ฐ€ ์žˆ๋‹ค.

{ "name": "์ž„๋š๋”ฑ", "id": "20210117", "like": ["์—ฌํ–‰", "๊ทธ๋ฆผ"] }

โ€‹

JSON ํ˜•์‹์œผ๋กœ ๋ฐ์ดํ„ฐ ํด๋ž˜์Šค ์ƒ์„ฑ

data class DataClassItem ( val name:String, val nickname:String, val age:Int?,//null ํ—ˆ์šฉ val like:ArrayList<String> )

โ€‹

DataClassItem์„ ์›์†Œ๋กœ ๊ฐ€์ง„ ArrayList ์ž๋ฃŒํ˜• ๋ฐ์ดํ„ฐ ํด๋ž˜์Šค

data class DataClass : ArrayList<DataClassItem>()

โ€‹

โ€‹

โ€‹

โ€‹

โ€‹

โ€‹

โ€‹

โ€‹

<๊ณต๋ถ€ ์ž๋ฃŒ>

https://blog.yena.io/studynote/2020/01/08/Android-Kotlin-Retrofit.html

โ˜… Retrofit + OkHttp

https://kor45cw.tistory.com/5

โ˜… Retrofit, HTTP Annotation

https://square.github.io/retrofit/

โ˜… Retrofit ๊ณต์‹๋ฌธ์„œ

https://codingnotes.tistory.com/73

https://github.com/HwangEunmi/Retrofit-Sample#@Body

https://abss.tistory.com/155

https://medium.com/@joycehong0524/android-studio-retrofit2-%EA%B8%B0%EB%B3%B8-%EC%82%AC%EC%9A%A9%EB%B2%95-retrofit-%EC%9D%98%EB%AC%B8%EC%A0%90-%ED%92%80%EC%96%B4%ED%97%A4%EC%B9%98%EA%B8%B0-%EC%8A%A4%EC%95%95-f150db436add

https://kumgo1d.tistory.com/m/2

https://mommoo.tistory.com/60

https://mcflynn.tistory.com/10

https://zzandoli.tistory.com/48

โ€‹

728x90