{"id":540,"date":"2018-07-01T23:36:22","date_gmt":"2018-07-01T14:36:22","guid":{"rendered":"http:\/\/www.typea.info\/blog\/index.php\/2018\/07\/01\/android_kotlin_retrofit2_web_api_json\/"},"modified":"2018-07-01T23:36:22","modified_gmt":"2018-07-01T14:36:22","slug":"android_kotlin_retrofit2_web_api_json","status":"publish","type":"post","link":"https:\/\/www.typea.info\/blog\/index.php\/2018\/07\/01\/android_kotlin_retrofit2_web_api_json\/","title":{"rendered":"Android Kotlin \u3067 Retrofit2 \u3092\u4f7f\u3063\u3066 Web API JSON\u51e6\u7406"},"content":{"rendered":"<p><a href=\"https:\/\/www.amazon.co.jp\/Kotlin%E3%82%B9%E3%82%BF%E3%83%BC%E3%83%88%E3%83%96%E3%83%83%E3%82%AF-%E6%96%B0%E3%81%97%E3%81%84Android%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9F%E3%83%B3%E3%82%B0-%E9%95%B7%E6%BE%A4-%E5%A4%AA%E9%83%8E\/dp\/4865940391\/ref=as_li_ss_il?ie=UTF8&amp;linkCode=li2&amp;tag=typea09-22&amp;linkId=091173c1bbde5bf1ceaa07bc271efda7\" target=\"_blank\"><img decoding=\"async\" src=\"\/\/ws-fe.amazon-adsystem.com\/widgets\/q?_encoding=UTF8&amp;ASIN=4865940391&amp;Format=_SL160_&amp;ID=AsinImage&amp;MarketPlace=JP&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=typea09-22\" border=\"0\"><\/a><img loading=\"lazy\" decoding=\"async\" width=\"1\" height=\"1\" style=\"margin: 0px !important; border: currentcolor !important; border-image: none !important !important;\" alt=\"\" src=\"https:\/\/ir-jp.amazon-adsystem.com\/e\/ir?t=typea09-22&amp;l=li2&amp;o=9&amp;a=4865940391\" border=\"0\">\n<\/p>\n<p><a href=\"https:\/\/amzn.to\/2tJrWxZ\">Kotlin\u30b9\u30bf\u30fc\u30c8\u30d6\u30c3\u30af<\/a> \u3092\u53c2\u8003\u306b\u3001Android \u304a\u3088\u3073 Java \u3067\u5229\u7528\u3067\u304d\u308b\u3001HTTP \u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u3001<a href=\"http:\/\/square.github.io\/retrofit\/\">Retrofit<\/a> \u3092\u4f7f\u7528\u3057\u3066\u3001Web API (JSON)\u3092 Kotlin\u306e\u30e2\u30c7\u30eb\u3068\u3057\u3066\u6271\u3046\u3002<\/p>\n<p>\u9a5a\u304f\u307b\u3069\u7c21\u5358\u3002<a href=\"https:\/\/amzn.to\/2tJrWxZ\">Kotlin\u30b9\u30bf\u30fc\u30c8\u30d6\u30c3\u30af<\/a>\u306fAndroid\u30a2\u30d7\u30ea\u3092Kotlin\u3067\u4f5c\u6210\u3059\u308b\u305f\u3081\u306e\u8981\u70b9\u304c\u7aef\u7684\u306b\u307e\u3068\u3081\u3089\u308c\u3066\u3044\u308b\u3002\u500b\u4eba\u7684\u306b\u306f\u3082\u3046\u5c11\u3057\u6587\u7ae0\u5bc6\u5ea6\u304c\u9ad8\u3044\u307b\u3046\u304c\u597d\u307f\u3060\u304c\u3001\u624b\u6cd5\u3092\u30b7\u30f3\u30d7\u30eb\u306b\u77e5\u308b\u306b\u306f\u3053\u3061\u3089\u306e\u307b\u3046\u304c\u826f\u3044\u304b\u3082\u3002<\/p>\n<h2>1.build.gradle (Module app)<\/h2>\n<p>build.gradle \u306b\u4ee5\u4e0b\u3092\u8ffd\u52a0\u3001retrofit\u304b\u3089\u3001\u30ea\u30a2\u30af\u30c6\u30a3\u30d6\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306e<a href=\"https:\/\/github.com\/ReactiveX\/RxJava\">RxJava<\/a>\u3001Google\u304c\u63d0\u4f9b\u3059\u308bJSON\u3068Java\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u76f8\u4e92\u5909\u63db\u30e9\u30a4\u30d6\u30e9\u30ea<a href=\"https:\/\/github.com\/google\/gson\">GSON<\/a>\u306e\u8a2d\u5b9a\u3092\u884c\u3046<\/p>\n<p><pre class=\"prettyprint linenums\">dependencies {\r\n            :\r\n    def retrofitVersion = '2.4.0'\r\n    compile \"com.squareup.retrofit2:retrofit:$retrofitVersion\"\r\n    compile \"com.squareup.retrofit2:adapter-rxjava:$retrofitVersion\"\r\n    compile \"com.squareup.retrofit2:converter-gson:$retrofitVersion\"\r\n}\r\n\r\n<\/pre>\n<h2>2.Model \u306e\u5b9a\u7fa9<\/h2>\n<p><pre class=\"prettyprint linenums\">data class Category(\r\n        val id: String,\r\n        val name: String,\r\n        val subCategoryList: List<subcategory>,\r\n        val priority: Int\r\n)\r\n\r\ndata class SubCategory(\r\n        val id: String,\r\n        val name: String,\r\n        val priority: Int\r\n)<\/subcategory><\/pre>\n<h2>3.API\u306e\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u3092\u5b9a\u7fa9<\/h2>\n<pre class=\"prettyprint linenums\">interface ApiClient {\r\n    @GET(\"\/category\")\r\n    fun category(@Query(\"query\") query: String) : Observable&lt;List&lt;Category&gt;&gt;\r\n}\r\n<\/pre>\n<h2>4.API\u547c\u3073\u51fa\u3057\u4f8b<\/h2>\n<p>\u4e0b\u8a18\u3067\u8a18\u8ff0\u3059\u308bJSON\u3092\u53d6\u5f97\u3059\u308b\u3001Web\u30b5\u30fc\u30d3\u30b9\u3092\u547c\u3073\u51fa\u3057\u3001\u53d6\u5f97\u51fa\u6765\u305f\u3089Logcat\u306b\u5185\u5bb9\u3092\u5410\u304d\u51fa\u3059\u3002<\/p>\n<pre class=\"prettyprint linenums\">private fun apiCategoryFetch() {\r\n    val gson = GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create()\r\n    val retrofit = Retrofit.Builder()\r\n            .baseUrl(CATEGORY_API_URL)\r\n            .addConverterFactory(GsonConverterFactory.create(gson))\r\n            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())\r\n            .build()\r\n    val apiClient = retrofit.create(ApiClient::class.java)\r\n    apiClient.category(\"\")\r\n            .subscribeOn(Schedulers.io())\r\n            .subscribe({\r\n                for (test in it) {\r\n                    Log.i(TAG, String.format(\"%s\", test))\r\n                }\r\n            }, {\r\n                Log.w(TAG, \"\")\r\n            })\r\n}<\/pre>\n<h2>5.JSON \u30b5\u30f3\u30d7\u30eb<\/h2>\n<p>\u4ee5\u4e0b\u306eJSON\u3092\u53d6\u5f97\u3059\u308b\u3002<\/p>\n<pre class=\"prettyprint linenums\">[\r\n  {\r\n    \"id\": \"690d2606-27a3-4fc5-96e9-a8c614c891da\", \r\n    \"name\": \"Category[0]\", \r\n    \"priority\": 0, \r\n    \"sub_category_list\": [\r\n      {\r\n        \"id\": \"1ac76014-d5f5-489e-bb7d-692a24544fd9\", \r\n        \"name\": \"SubCategory[0,0]\", \r\n        \"priority\": 0\r\n      }, \r\n      {\r\n        \"id\": \"69b5c1ab-59f9-4d73-8b26-1de678aa6fa9\", \r\n        \"name\": \"SubCategory[0,2]\", \r\n        \"priority\": 2\r\n      }, \r\n      {\r\n        \"id\": \"a6be8f99-653d-4eea-8cf5-0add0133bbb2\", \r\n        \"name\": \"SubCategory[0,1]\", \r\n        \"priority\": 1\r\n      }\r\n    ]\r\n  }, \r\n  {\r\n    \"id\": \"067a1604-d9da-4676-9c2f-7a0d1dbed6ec\", \r\n    \"name\": \"Category[1]\", \r\n    \"priority\": 1, \r\n    \"sub_category_list\": [\r\n      {\r\n        \"id\": \"4f5c8387-4b5e-47b5-9a3f-7890ba7ea840\", \r\n        \"name\": \"SubCategory[1,0]\", \r\n        \"priority\": 0\r\n      }, \r\n      {\r\n        \"id\": \"e789a851-8b06-4612-9ff3-fa4a70b15df9\", \r\n        \"name\": \"SubCategory[1,2]\", \r\n        \"priority\": 2\r\n      }, \r\n      {\r\n        \"id\": \"c1d66227-22b0-4fab-8500-953c78bb7a84\", \r\n        \"name\": \"SubCategory[1,1]\", \r\n        \"priority\": 1\r\n      }\r\n    ]\r\n  }, \r\n  {\r\n    \"id\": \"c4824082-d727-4dcb-9341-8f734d83dcf2\", \r\n    \"name\": \"Category[2]\", \r\n    \"priority\": 2, \r\n    \"sub_category_list\": [\r\n      {\r\n        \"id\": \"7d4e1da9-604e-47e9-9d64-4d0831d3335d\", \r\n        \"name\": \"SubCategory[2,0]\", \r\n        \"priority\": 0\r\n      }, \r\n      {\r\n        \"id\": \"9ff87596-f129-4bc0-bfac-427546e58809\", \r\n        \"name\": \"SubCategory[2,2]\", \r\n        \"priority\": 2\r\n      }, \r\n      {\r\n        \"id\": \"2ad260c8-9d11-4188-b596-49bc98e4987f\", \r\n        \"name\": \"SubCategory[2,1]\", \r\n        \"priority\": 1\r\n      }\r\n    ]\r\n  }\r\n]<\/pre>\n<h2>6.\u53d6\u5f97\u7d50\u679c\u306eLog<\/h2>\n<p>\u3044\u3044\u611f\u3058\u3067\u53d6\u5f97\u3067\u304d\u305f\u3002<\/p>\n<pre class=\"prettyprint linenums\">07-01 23:16:09.976 22795-22849\/info.typea.hoge I\/@@@@@ MainActivity: Category(id=690d2606-27a3-4fc5-96e9-a8c614c891da, name=Category[0], subCategoryList=[SubCategory(id=1ac76014-d5f5-489e-bb7d-692a24544fd9, name=SubCategory[0,0], priority=0), SubCategory(id=69b5c1ab-59f9-4d73-8b26-1de678aa6fa9, name=SubCategory[0,2], priority=2), SubCategory(id=a6be8f99-653d-4eea-8cf5-0add0133bbb2, name=SubCategory[0,1], priority=1)], priority=0)\r\n    Category(id=067a1604-d9da-4676-9c2f-7a0d1dbed6ec, name=Category[1], subCategoryList=[SubCategory(id=4f5c8387-4b5e-47b5-9a3f-7890ba7ea840, name=SubCategory[1,0], priority=0), SubCategory(id=e789a851-8b06-4612-9ff3-fa4a70b15df9, name=SubCategory[1,2], priority=2), SubCategory(id=c1d66227-22b0-4fab-8500-953c78bb7a84, name=SubCategory[1,1], priority=1)], priority=1)\r\n07-01 23:16:09.986 22795-22849\/info.typea.sentence2 I\/@@@@@ MainActivity: Category(id=c4824082-d727-4dcb-9341-8f734d83dcf2, name=Category[2], subCategoryList=[SubCategory(id=7d4e1da9-604e-47e9-9d64-4d0831d3335d, name=SubCategory[2,0], priority=0), SubCategory(id=9ff87596-f129-4bc0-bfac-427546e58809, name=SubCategory[2,2], priority=2), SubCategory(id=2ad260c8-9d11-4188-b596-49bc98e4987f, name=SubCategory[2,1], priority=1)], priority=2)\r\n<\/pre>\n<p><a href=\"https:\/\/www.amazon.co.jp\/%E5%BF%9C%E7%94%A8%E5%8A%9B%E3%82%92%E3%81%97%E3%81%A3%E3%81%8B%E3%82%8A%E8%82%B2%E6%88%90-Android%E3%82%A2%E3%83%97%E3%83%AA%E9%96%8B%E7%99%BA%E3%81%AE%E6%95%99%E7%A7%91%E6%9B%B8-%E3%81%AA%E3%82%93%E3%81%A1%E3%82%83%E3%81%A3%E3%81%A6%E9%96%8B%E7%99%BA%E8%80%85%E3%81%AB%E3%81%AA%E3%82%89%E3%81%AA%E3%81%84%E3%81%9F%E3%82%81%E3%81%AE%E5%AE%9F%E8%B7%B5%E3%83%8F%E3%83%B3%E3%82%BA%E3%82%AA%E3%83%B3-CodeZine-BOOKS\/dp\/4798152021\/ref=as_li_ss_il?_encoding=UTF8&amp;psc=1&amp;refRID=JY3M41CH73Q0N2PAWHNA&amp;linkCode=li2&amp;tag=typea09-22&amp;linkId=7be376dd56c7d93fd589bb17387af0f0\" target=\"_blank\"><img decoding=\"async\" src=\"\/\/ws-fe.amazon-adsystem.com\/widgets\/q?_encoding=UTF8&amp;ASIN=4798152021&amp;Format=_SL160_&amp;ID=AsinImage&amp;MarketPlace=JP&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=typea09-22\" border=\"0\"><\/a><img loading=\"lazy\" decoding=\"async\" width=\"1\" height=\"1\" style=\"margin: 0px !important; border: currentcolor !important; border-image: none !important;\" alt=\"\" src=\"https:\/\/ir-jp.amazon-adsystem.com\/e\/ir?t=typea09-22&amp;l=li2&amp;o=9&amp;a=4798152021\" border=\"0\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Kotlin\u30b9\u30bf\u30fc\u30c8\u30d6\u30c3\u30af \u3092\u53c2\u8003\u306b\u3001Android \u304a\u3088\u3073 Java \u3067\u5229\u7528\u3067\u304d\u308b\u3001HTTP \u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u3001Retrofit \u3092\u4f7f\u7528\u3057\u3066\u3001Web API (JSON)\u3092 Kotlin\u306e\u30e2\u30c7\u30eb\u3068\u3057\u3066\u6271\u3046\u3002 \u9a5a\u304f\u307b\u3069\u7c21\u5358\u3002 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"sns_share_botton_hide":"","vkExUnit_sns_title":"","_vk_print_noindex":"","sitemap_hide":"","_veu_custom_css":"","veu_display_promotion_alert":"","vkexunit_cta_each_option":"","footnotes":""},"categories":[39,105],"tags":[],"class_list":["post-540","post","type-post","status-publish","format-standard","hentry","category-android","category-kotlin"],"veu_head_title_object":{"title":"","add_site_title":""},"_links":{"self":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/540","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=540"}],"version-history":[{"count":0,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/540\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=540"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=540"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=540"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}