\uc548\ub4dc\ub85c\uc774\ub4dc \uc571\uc5d0\uc11c \uc11c\ubc84\uc640\uc758 \ud1b5\uc2e0\uc740 \ub9e4\uc6b0 \uc911\uc694\ud55c \uc694\uc18c\uc785\ub2c8\ub2e4. \uc774\ub97c \uc704\ud574 REST API\ub97c \uc0ac\uc6a9\ud574\uc57c \ud558\uba70, \uc774\ub97c \ub354\uc6b1 \uc27d\uac8c \uad6c\ud604\ud560 \uc218 \uc788\ub294 Retrofit \ub77c\uc774\ube0c\ub7ec\ub9ac\uac00 \uc788\uc2b5\ub2c8\ub2e4. \uc774\ubc88 \uae00\uc5d0\uc11c\ub294 \uc548\ub4dc\ub85c\uc774\ub4dc\uc5d0\uc11c REST API \ud1b5\uc2e0\uc758 \ud544\uc694\uc131\uacfc Retrofit \ub77c\uc774\ube0c\ub7ec\ub9ac\uc758 \uac1c\uc694\uc640 \uc7a5\uc810, \uadf8\ub9ac\uace0 \uc548\ub4dc\ub85c\uc774\ub4dc\uc5d0\uc11c \uc774\ub97c \ud65c\uc6a9\ud558\ub294 \ubc29\ubc95\uc744 \uc54c\uc544\ubcf4\uaca0\uc2b5\ub2c8\ub2e4.<\/p>\n
\uc548\ub4dc\ub85c\uc774\ub4dc \uc571\uc5d0\uc11c \uc11c\ubc84\uc640 \ud1b5\uc2e0\ud574\uc57c \ud558\ub294 \uacbd\uc6b0\uac00 \ub9ce\uc2b5\ub2c8\ub2e4. \uc608\ub97c \ub4e4\uba74, \ub85c\uadf8\uc778, \ud68c\uc6d0\uac00\uc785, \ub370\uc774\ud130 \uc870\ud68c \ub4f1\uc774 \uc788\uc2b5\ub2c8\ub2e4. \uc774\ub54c REST API\ub97c \uc0ac\uc6a9\ud558\uba74 \uac04\ub2e8\ud558\uac8c \uc11c\ubc84\uc640 \ud1b5\uc2e0\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4. REST API\ub294 HTTP \ud504\ub85c\ud1a0\ucf5c\uc744 \uc0ac\uc6a9\ud558\uba70, GET, POST, PUT, DELETE \ub4f1\uc758 HTTP \uba54\uc11c\ub4dc\ub97c \uc0ac\uc6a9\ud558\uc5ec \ub370\uc774\ud130\ub97c \uc694\uccad\ud558\uace0 \uc751\ub2f5\ud569\ub2c8\ub2e4.<\/p>\n
Retrofit\uc740 Square\uc5d0\uc11c \ub9cc\ub4e0 \uc548\ub4dc\ub85c\uc774\ub4dc\uc6a9 REST API \ud1b5\uc2e0 \ub77c\uc774\ube0c\ub7ec\ub9ac\uc785\ub2c8\ub2e4. Retrofit\uc740 \uc571\uc5d0\uc11c REST API\ub97c \ud638\ucd9c\ud558\uace0 \uc751\ub2f5\uc744 \ucc98\ub9ac\ud558\ub294 \ucf54\ub4dc\ub97c \uc791\uc131\ud558\ub294 \uac83\uc744 \ud06c\uac8c \ub2e8\uc21c\ud654\ud574\uc90d\ub2c8\ub2e4. \uc774\ub97c \uc704\ud574 \uc778\ud130\ud398\uc774\uc2a4\ub97c \uc0ac\uc6a9\ud558\uba70, \uc778\ud130\ud398\uc774\uc2a4\uc758 \uba54\uc11c\ub4dc\ub97c \ud638\ucd9c\ud558\uba74 Retrofit\uc774 \uc790\ub3d9\uc73c\ub85c HTTP \uc694\uccad\uc744 \ub9cc\ub4e4\uc5b4 \uc11c\ubc84\ub85c \ubcf4\ub0b4\uace0, \uc11c\ubc84\uc5d0\uc11c \uc751\ub2f5\uc744 \ubc1b\uc73c\uba74 \uc790\ub3d9\uc73c\ub85c \ud30c\uc2f1\ud569\ub2c8\ub2e4. \uc774\ub97c \ud1b5\ud574 \uac1c\ubc1c\uc790\uac00 \uc9c1\uc811 HTTP \uc694\uccad\uc744 \ub9cc\ub4e4\uace0 \ud30c\uc2f1\ud558\ub294 \ucf54\ub4dc\ub97c \uc791\uc131\ud558\uc9c0 \uc54a\uc544\ub3c4 \ub429\ub2c8\ub2e4.<\/p>\n
Retrofit\uc758 \uc7a5\uc810\uc740 \ub2e4\uc74c\uacfc \uac19\uc2b5\ub2c8\ub2e4.<\/p>\n
Retrofit\uc744 \uc0ac\uc6a9\ud558\uae30 \uc704\ud574\uc11c\ub294 \uba3c\uc800 Gradle \ud30c\uc77c\uc5d0 \uc758\uc874\uc131\uc744 \ucd94\uac00\ud574\uc57c \ud569\ub2c8\ub2e4. \uc758\uc874\uc131\uc744 \ucd94\uac00\ud558\uba74 Retrofit\uacfc Gson, OkHttp \ub77c\uc774\ube0c\ub7ec\ub9ac\uac00 \ud568\uaed8 \ucd94\uac00\ub429\ub2c8\ub2e4. \ub2e4\uc74c\uc740 \uc758\uc874\uc131 \ucd94\uac00 \ucf54\ub4dc\uc785\ub2c8\ub2e4.<\/p>\n
dependencies {\n implementation 'com.squareup.retrofit2:retrofit:2.9.0'\n implementation 'com.squareup.retrofit2:converter-gson:2.9.0'\n implementation 'com.squareup.okhttp3:okhttp:4.9.1'\n}<\/code><\/pre>\n\uc758\uc874\uc131\uc744 \ucd94\uac00\ud55c \ud6c4\uc5d0\ub294 Retrofit\uc744 \uc0ac\uc6a9\ud560 \uc778\ud130\ud398\uc774\uc2a4\ub97c \uc791\uc131\ud574\uc57c \ud569\ub2c8\ub2e4. \uc778\ud130\ud398\uc774\uc2a4\uc5d0\uc11c\ub294 HTTP \uc694\uccad\uc744 \uc815\uc758\ud558\uba70, \uc774\ub97c \ud1b5\ud574 Retrofit\uc774 \uc11c\ubc84\uc640 \ud1b5\uc2e0\ud569\ub2c8\ub2e4. \ub2e4\uc74c\uc740 \uc778\ud130\ud398\uc774\uc2a4 \uc608\uc2dc\uc785\ub2c8\ub2e4.<\/p>\n
public interface ApiService {\n @GET(\"users\/{user}\")\n Call getUser(@Path(\"user\") String userId);\n}<\/code><\/pre>\n\uc778\ud130\ud398\uc774\uc2a4\uc5d0\uc11c\ub294 @GET<\/code>, @POST<\/code>, @PUT<\/code>, @DELETE<\/code> \ub4f1\uc758 \uc5b4\ub178\ud14c\uc774\uc158\uc744 \uc0ac\uc6a9\ud558\uc5ec HTTP \uba54\uc11c\ub4dc\ub97c \uc815\uc758\ud569\ub2c8\ub2e4. {}<\/code>\ub85c \ub458\ub7ec\uc2f8\uc778 \ubd80\ubd84\uc740 \ub3d9\uc801\uc73c\ub85c \ubcc0\uacbd\ub420 \uc218 \uc788\ub294 \ubd80\ubd84\uc73c\ub85c, @Path<\/code> \uc5b4\ub178\ud14c\uc774\uc158\uc744 \uc0ac\uc6a9\ud558\uc5ec \uac12\uc744 \uc804\ub2ec\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4. \uc608\ub97c \ub4e4\uc5b4, \uc704\uc758 \ucf54\ub4dc\uc5d0\uc11c\ub294 users\/{user}<\/code>\uc5d0\uc11c {user}<\/code> \ubd80\ubd84\uc774 \ub3d9\uc801\uc73c\ub85c \ubcc0\uacbd\ub429\ub2c8\ub2e4.<\/p>\n
\uc778\ud130\ud398\uc774\uc2a4\ub97c \uc791\uc131\ud55c \ud6c4\uc5d0\ub294 Retrofit \uac1d\uccb4\ub97c \uc0dd\uc131\ud574\uc57c \ud569\ub2c8\ub2e4. \ub2e4\uc74c\uc740 Retrofit \uac1d\uccb4 \uc0dd\uc131 \ucf54\ub4dc\uc785\ub2c8\ub2e4.<\/p>\n
Retrofit retrofit = new Retrofit.Builder()\n .baseUrl(\"https:\/\/api.github.com\/\")\n .addConverterFactory(GsonConverterFactory.create())\n .build();\n\nApiService service = retrofit.create(ApiService.class);<\/code><\/pre>\nRetrofit \uac1d\uccb4\ub97c \uc0dd\uc131\ud560 \ub54c baseUrl<\/code>\uc744 \uc124\uc815\ud574\uc57c \ud569\ub2c8\ub2e4. \uc774\ub294 \uc11c\ubc84\uc758 \uae30\ubcf8 URL\uc744 \uc758\ubbf8\ud569\ub2c8\ub2e4. addConverterFactory<\/code>\ub97c \uc0ac\uc6a9\ud558\uc5ec JSON \ud30c\uc2f1 \ub77c\uc774\ube0c\ub7ec\ub9ac\ub97c \uc124\uc815\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4. \uc774\ud6c4 create<\/code> \uba54\uc11c\ub4dc\ub97c \ud638\ucd9c\ud558\uc5ec \uc778\ud130\ud398\uc774\uc2a4 \uad6c\ud604\uccb4\ub97c \uc0dd\uc131\ud569\ub2c8\ub2e4.<\/p>\n
\uc774\uc81c \uc778\ud130\ud398\uc774\uc2a4\uc5d0\uc11c \uc815\uc758\ud55c \uba54\uc11c\ub4dc\ub97c \ud638\ucd9c\ud558\uba74 \ub429\ub2c8\ub2e4. \ub2e4\uc74c\uc740 \uc608\uc2dc \ucf54\ub4dc\uc785\ub2c8\ub2e4.<\/p>\n
Call call = service.getUser(\"octocat\");\ncall.enqueue(new Callback() {\n @Override\n public void onResponse(Call call, Response response) {\n User user = response.body();\n Log.d(\"Retrofit\", \"user: \" + user);\n }\n\n @Override\n public void onFailure(Call call, Throwable t) {\n Log.e(\"Retrofit\", \"error: \" + t.getMessage());\n }\n});<\/code><\/pre>\nenqueue<\/code> \uba54\uc11c\ub4dc\ub97c \ud638\ucd9c\ud558\uc5ec \ube44\ub3d9\uae30\uc801\uc73c\ub85c HTTP \uc694\uccad\uc744 \uc2e4\ud589\ud569\ub2c8\ub2e4. \uc774\ud6c4 onResponse<\/code> \ub610\ub294 onFailure<\/code> \ucf5c\ubc31 \uba54\uc11c\ub4dc\uc5d0\uc11c \uacb0\uacfc\ub97c \ucc98\ub9ac\ud569\ub2c8\ub2e4.<\/p>\n
\uc548\ub4dc\ub85c\uc774\ub4dc \uc571\uc5d0\uc11c REST API\ub97c \uc0ac\uc6a9\ud558\uc5ec \uc11c\ubc84\uc640 \ud1b5\uc2e0\ud558\ub294 \ubc29\ubc95\uc744 \uc54c\uc544\ubcf4\uc558\uc2b5\ub2c8\ub2e4. Retrofit \ub77c\uc774\ube0c\ub7ec\ub9ac\ub97c \uc0ac\uc6a9\ud558\uba74 \uac04\ub2e8\ud558\uac8c REST API\ub97c \ud638\ucd9c\ud560 \uc218 \uc788\uc73c\uba70, Gson \ub4f1\uc758 JSON \ud30c\uc11c\uc640 \ud568\uaed8 \uc0ac\uc6a9\ud558\uba74 JSON \ub370\uc774\ud130\ub97c \uc27d\uac8c \ud30c\uc2f1\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4. Retrofit\uc740 \uc548\ub4dc\ub85c\uc774\ub4dc \uc571\uc5d0\uc11c \uc11c\ubc84\uc640\uc758 \ud1b5\uc2e0\uc744 \ub354\uc6b1 \uc27d\uace0 \ud3b8\ub9ac\ud558\uac8c \ub9cc\ub4e4\uc5b4\uc90d\ub2c8\ub2e4.<\/p>\n","protected":false},"excerpt":{"rendered":"
\uc548\ub4dc\ub85c\uc774\ub4dc\uc5d0\uc11c REST API \ud1b5\uc2e0\uc744 \ud560 \ub54c Retrofit \ub77c\uc774\ube0c\ub7ec\ub9ac\ub97c \uc0ac\uc6a9\ud558\uba74 \ud3b8\ub9ac\ud558\ub2e4.<\/p>\n","protected":false},"author":1,"featured_media":12633,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1957],"tags":[2119,3688,2188,2291,2580],"class_list":["post-32725","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development","tag-api","tag-path","tag-public","tag-service","tag-user"],"acf":[],"_links":{"self":[{"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/posts\/32725","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/comments?post=32725"}],"version-history":[{"count":1,"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/posts\/32725\/revisions"}],"predecessor-version":[{"id":33301,"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/posts\/32725\/revisions\/33301"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/media\/12633"}],"wp:attachment":[{"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/media?parent=32725"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/categories?post=32725"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/m9js.shop\/blog\/wp-json\/wp\/v2\/tags?post=32725"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}