๊นจ์•Œ ๊ฐœ๋… ๐Ÿ“‘/Android

[Android] Retrofit - ๋ฐฐ๊ฒฝ๊ณผ ๊ตฌ์กฐ

interfacer_han 2024. 5. 28. 15:45

#1 Restrofit์˜ ๋ฐฐ๊ฒฝ

#1-1 REST API

 

REST API (REpresentational State Transfer Application Programming Interface)

#1 ๋ฌด์—‡(What)์— ๋Œ€ํ•œ API์ธ๊ฐ€?#1-1 ๊ฐœ์š”REST(REpresentational State Transfer) ๋˜๋Š” RESTful API๋Š” ์„œ๋ฒ„์˜ ์ž์›(Resource)์„ ๋‘๊ณ , ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„ ๊ฐ„์˜ ํ†ต์‹  ๋ฐฉ๋ฒ•์„ ๊ทœ์ •ํ•˜๋Š” API(Application Programming Interface)๋‹ค. ์ž

kenel.tistory.com

๋จผ์ €, ํด๋ผ์ด์–ธํŠธ-์„œ๋ฒ„ ๊ฐ„ ํ†ต์‹ ์—์„œ ์‚ฌ์šฉ๋˜๋Š” REST API์— ๋Œ€ํ•ด ์•Œ์•„์•ผ ํ•œ๋‹ค.

 

#1-2 OkHttp

 

Overview - OkHttp

OkHttp HTTP is the way modern applications network. It’s how we exchange data & media. Doing HTTP efficiently makes your stuff load faster and saves bandwidth. OkHttp is an HTTP client that’s efficient by default: HTTP/2 support allows all requests to

square.github.io

์˜ˆ์ „ ์•ˆ๋“œ๋กœ์ด๋“œ์—์„œ๋Š” ํด๋ผ์ด์–ธํŠธ-์„œ๋ฒ„ ๊ฐ„ ํ†ต์‹ ์„ ์œ„ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ HTTP ํ”„๋กœํ† ์ฝœ์„ ์‚ฌ์šฉํ•˜๋Š” OkHttp๋ฅผ ์ผ์—ˆ๋‹ค.

 

#1-3 Restrofit

 

Retrofit

A type-safe HTTP client for Android and Java

square.github.io

์ดํ›„, Retrofit์ด ์ถœ์‹œ๋๋‹ค. Retrofit์€ OkHttp์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์„ค๊ณ„๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์—, ๋‚ด๋ถ€์ ์œผ๋กœ๋Š” OkHttp๊ณผ ๋˜‘๊ฐ™์ด ์ž‘๋™ํ•œ๋‹ค. ํ•˜์ง€๋งŒ, Retrofit์€ OkHttp์— ๋น„ํ•ด ๋” ์‰ฝ๊ณ  ํšจ์œจ์ ์ด๋ฉฐ REST API์— ์นœํ™”์ ์ด๋‹ค. 

 

#2 ์•ˆ๋“œ๋กœ์ด๋“œ์—์„œ์˜ Retrofit ๊ตฌ์กฐ

#2-1 ๋„์‹๋„์™€ ์ข…์†์„ฑ

ํ™”์‚ดํ‘œ๋Š” ํด๋ž˜์Šค ๊ฐ„์˜ ์ข…์†์„ฑ์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, Retrofit Instance๋Š” Service Interface์— ์ข…์†๋œ๋‹ค. ์ข…์†์˜ ์‚ฌ์ „์  ์˜๋ฏธ๋Š” '์ž์ฃผ์„ฑ์ด ์—†์ด ์ฃผ๊ฐ€ ๋˜๋Š” ๊ฒƒ์— ๋”ธ๋ ค ๋ถ™์Œ'์ด๋‹ค. ์ข…์†์€ '์•Œ์•„์•ผ ํ•œ๋‹ค'๋ผ๋Š” ๋ง๋กœ๋„ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ Retrofit Instance๋Š” Service Interface์— ๋Œ€ํ•ด ์•Œ์•„์•ผ ํ•œ๋‹ค. ๋ฐ˜๋ฉด, Service Interface Retrofit Instance๋ฅผ ๋ชฐ๋ผ๋„ ๋œ๋‹ค. Service Interface์„ ์„ค๊ณ„ํ•  ๋• Retrofit Instance์—์„œ ๋ญ˜ ์–ด๋–ป๊ฒŒ ํ• ์ง€ ์ „ํ˜€ ์‹ ๊ฒฝ์“ฐ์ง€ ์•Š์•„๋„ ๋œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค (๋Œ€์‹ , Service Interface์€ Data class์— ๋Œ€ํ•ด ์ข…์†์ ์ด๋ฏ€๋กœ Data class๋ฅผ ์ฐธ์กฐํ•˜๋ฉฐ ์„ค๊ณ„ํ•ด์•ผ ํ•œ๋‹ค). Retrofit Instance๋ฅผ ์„ค๊ณ„ํ•  ๋• Service Interface์„ ์•Œ์•„์•ผ ํ•œ๋‹ค. '์•Œ์•„์•ผ ํ•˜๋Š” ์ชฝ'์—์„œ '๋ชฐ๋ผ๋„ ๋˜๋Š” ์ชฝ'์œผ๋กœ ํ™”์‚ดํ‘œ๋ฅผ ์ด์€ ๊ฒƒ์ด ์œ„ ๋„์‹๋„๋‹ค.

 

์•„๋ž˜๋Š” ๊ฐ ์š”์†Œ์— ๋Œ€ํ•œ ์„ค๋ช…์ด๋‹ค.

 

#2-2 Data class

REST API๋Š” ์ž์›(Resource)์— ๊ธฐ๋ฐ˜ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ, ์„œ๋ฒ„์™€ ์ฃผ๊ณ  ๋ฐ›์„ ์ž์›์„ data class์˜ ํ˜•ํƒœ๋กœ ๋จผ์ € ์ •์˜ํ•œ๋‹ค.

 

#2-3 Service Interface

HTTP ๋ฉ”์†Œ๋“œ์™€ ํ•จ๊ป˜ ๋‹ค๋ฃฐ ์ž์›์˜ ์œ„์น˜(์ด ๊ฒŒ์‹œ๊ธ€์˜ #2-2 ์ฐธ์กฐ)๋ฅผ ๋ฉ”์†Œ๋“œ์˜ ํ˜•ํƒœ๋กœ ํ•˜๋‚˜์”ฉ ๊ธฐ์ˆ ํ•œ๋‹ค.

 

#2-4 Retrofit Instance

Retrofit์˜ ์ธ์Šคํ„ด์Šค๋Š” Retrofit.Builder()๋ผ๋Š” ๋นŒ๋” ํด๋ž˜์Šค๋ฅผ ํ†ตํ•ด ์ƒ์„ฑ๋œ๋‹ค. Service Interface์—์„œ ๊ธฐ์ˆ ํ–ˆ๋˜ '๋‹ค๋ฃฐ ์ž์›์˜ ์œ„์น˜'์— ๋Œ€ํ•ด ๊ทธ ๊ธฐ๋ฐ˜์ด ๋˜๋Š” ๊ธฐ๋ณธ URL๊ณผ ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ๋ฐ›์€ JSON ๋“ฑ์˜ ํŒŒ์ผ ํ˜•์‹์„ ์ฝ”ํ‹€๋ฆฐ ๊ฐ์ฒด๋กœ ๋ฐ”๊ฟ”์ฃผ๋Š” Converter๋ฅผ Retrofit.Builder()์— ๋‹ฌ์•„์ค€ ๋‹ค์Œ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

 

์ƒ์„ฑ๋œ Retrofit์˜ ์ธ์Šคํ„ด์Šค๋Š” Service Interface๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›๋Š” Retrofit.create() ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด Service Interface์˜ ๊ตฌํ˜„์ฒด(Service Class)๋ฅผ ๋งŒ๋“ ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ๊ตฌํ˜„์ฒด์˜ ๋ฉ”์†Œ๋“œ๋“ค(= Service Interface์—์„œ ์ •์˜ํ–ˆ๋˜ ๋ฉ”์†Œ๋“œ๋“ค)๋กœ ์„œ๋ฒ„์™€ ํ†ต์‹ ํ•œ๋‹ค.

 

#3 ์š”์•ฝ

Retrofit์€ ํด๋ผ์ด์–ธํŠธ-์„œ๋ฒ„ ๊ฐ„ ํ†ต์‹ ์„ ๊ฐ„ํŽธํžˆ ๊ตฌํ˜„ํ•˜๋Š” ๋„๊ตฌ๋‹ค.

 

#4 ์ด์–ด์ง€๋Š” ๊ธ€

 

[Android] Retrofit - ๊ธฐ์ดˆ

#1 ์ด์ „ ๊ธ€ [Android] Retrofit - ๋ฐฐ๊ฒฝ๊ณผ ๊ตฌ์กฐ#1 Restrofit์˜ ๋ฐฐ๊ฒฝ#1-1 REST API REST API (REpresentational State Transfer Application Programming Interface)#1 ๋ฌด์—‡(What)์— ๋Œ€ํ•œ API์ธ๊ฐ€?#1-1 ๊ฐœ์š”REST(REpresentational State Transfer) ๋˜๋Š”

kenel.tistory.com

์•ˆ๋“œ๋กœ์ด๋“œ ํ”„๋กœ์ ํŠธ์—์„œ Retrofit์„ ๊ตฌํ˜„ํ•ด๋ณธ๋‹ค.