#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์ ๊ตฌํํด๋ณธ๋ค.
'๊นจ์ ๊ฐ๋ ๐ > Android' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Android] Retrofit - MVVM ๊ตฌ์กฐ (0) | 2024.06.05 |
---|---|
[Android] Retrofit - ๊ธฐ์ด (0) | 2024.05.29 |
[Android] Room - AutoMigrationSpec (0) | 2024.05.08 |
[Android] Room - AutoMigration ๊ธฐ์ด (0) | 2024.05.06 |
[Android] Room - ๋ฐํ ๊ฐ์ด ์๋ INSERT (0) | 2024.02.29 |