Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Web Api Clientを考える #dotsandroid

9278c3a06b8d8752fb913dea93f959c1?s=47 FUJI Goro
August 08, 2015

Web Api Clientを考える #dotsandroid

Androidオールスターズ 2015/8/8 の資料です

9278c3a06b8d8752fb913dea93f959c1?s=128

FUJI Goro

August 08, 2015
Tweet

Transcript

  1. Web Api ClientΛߟ͑Δ AndroidΦʔϧελʔζ 2015/8/8 #dotsandroid @__gfx__ at Cookpad, Inc.

  2. Introduction to myself • github:gfx, twitter:__gfx__, FUJI Goro • ΫοΫύουॴଐ

    • ΫοΫύουΞϓϦͷϦϦʔε؅ཧɾϝϯ ςφϯεΛओʹ͍ͯ͠Δ • ࠲ӈͷ໏͸ʮਖ਼͍͠ίʔυΛॻ͜͏ɻಈ ͘ίʔυͰຬ଍ͤͣʹʯ
  3. ϥΠϒϥϦͷબఆํ਑ • Android൛ΫοΫύουΞϓϦͰ࠾༻͍ͯ͠Δٕज़ͷݱ ঢ়֬ೝ http://techlife.cookpad.com/entry/2015/06/25/ 093507 • “ΫοΫύουͷ΢ΣϒαʔϏεಉ༷ʹɺϞόΠϧΞϓ Ϧͷ։ൃ΋͜ͷઌ10೥Ҏ্ଓ͘Ͱ͠ΐ͏ɻ͕ٕͨͬͯ͠ ज़બ୒ͷجຊతͳࢦ਑ʹ͓͍ͯ͸ɺ͜ͷઌ10೥ؒܧଓ͠

    ͯϝϯςφϯε͓Αͼߋ৽͍ͯ͘͜͠ͱ͕Ͱ͖Δ͔Ͳ͏ ͔͕ॏཁͰ͢ɻ”
  4. Web Api Client

  5. ͳͥWeb Api Client͕ॏཁͳͷ͔ • ଟ͘ͷεϚϗΞϓϦ͸ɺWeb APIͷΠϯλʔϑΣΠ εʹ͗͢ͳ͍ • Cookpad΋ྫ֎Ͱ͸ͳ͘ɺΞϓϦ͸جຊతʹ JSONΛฦ͢Web

    API΁ͷΠϯλʔϑΣΠεͰ͋ Δ • Ώ͑ʹWeb Api ClientͰ࢖͏Http ClientΛԿʹ͢ Δ͔͸ඇৗʹॏཁ
  6. ༻ޠ • Web Api Client • υϝΠϯݻ༗ͷWeb ApiΛݺͼग़͢ΫϥεϥΠϒϥϦ • Picasso,

    GlideͳͲͷը૾μ΢ϯϩʔμ΋Web Api ClientͷҰछ • ௿ϨϕϧHttp Client • HTTPΛ࣮૷ͨ͠ΫϥεϥΠϒϥϦ • Apache HTTP client, HttpURLConnection, OkHttpͳͲ • ߴϨϕϧHttp Client • ௿ϨϕϧHttp ClientΛϥοϓͯ͠࢖͍΍ͨ͘͢͠ΫϥεϥΠϒϥϦ • Volley, RetrofitͳͲɻWeb Api Clientͱͷ۠ผ͸ᐆດͳ͜ͱ͕͋Δ
  7. ௿ϨϕϧHttp Clientੇධ

  8. Apache HTTP Client • Androidʹ૊Έࠐ·Εͨ௿ϨϕϧHttp Client • DEPRECATED! DEPRECATED! DEPRECATED!

    • ૊ΈࠐΈ൛ͱϦϦʔε൛ͷဃ཭͕େ͖͍ • multipart requestͳͲมΘͬͨ͜ͱΛ͢Δʹ͸૊ΈࠐΈ൛Ͱ͸଍ Γͣɺద੾ͳόʔδϣϯͷϦϦʔε൛ίϯ ϙʔωϯτΛ࢖͏ඞཁ͕͋Δ • ͳͥAndroid frameworkʹ૊ΈࠐΜͩͷͩ…
  9. HttpURLConnection • Javaඪ४ΫϥεϥΠϒϥϦͷHttp Client • యܕతͳ࢖͍ํͰΩϟετ͕ඞཁͱ͍͏ҟৗͳઃܭ • (HttpURLConnection) url.openConnection(); •

    ΠϯλʔϑΣΠε͸HTTPΛӅṭ͍͗ͯͯ͢͠෼͔Γʹ͍͘ • Android 4.4Ҏ߱͸όοΫΤϯυ࣮૷͕OkHttpʹͳͬͨ ͕ɺ͜Ε͕ΩϟογϡपΓʹόά͕͋ΓΫϥογϡ͢Δͷ Ͱ࢖Θͳ͍΄͏͕ແ೉
  10. OkHttp • Squareࣾ։ൃͷ௿ϨϕϧHttp Client • HTTP/2αϙʔτ • ಉظAPIͱඇಉظAPI྆ํ͋Δ • ͳͥ྆ํ༻ҙͨ͠͠…͜Εͷ͍ͤͰʢ͈́

    • Android૊ΈࠐΈͰͳ͍ͷ͕େ͖ͳϝϦοτ • ࣮ߦ͞ΕΔίʔυΛ׬શʹ༧ଌͰ͖Δʂʂʂ
  11. ཧ૝తͳ௿ϨϕϧHttp Client • ϚϧνεϨουΛѻΘͳ͍͜ͱ • ฒྻϦΫΤετͳͲ͸ΑΓ্ҐϨΠϠͰ΍Δ ΂͖ • ͨͩ͠εϨουηʔϑͰ͸͋Δ΂͖ •

    pure JavaͰ࣮૷͠ɺAndroid frameworkͷ Http Clientʹґଘ͠ͳ͍͜ͱ
  12. ߴϨϕϧHttp Clientੇධ

  13. Volley • 2013೥ͷGoogle I/O ʹͯክ੕ͷΑ͏ʹݱΕ ͨߴϨϕϧHttp Client • AndroidքͷελϯμʔυʹͳΔ͔ͱࢥΘΕͨ ͕…

  14. VolleyͷΑ͍ͱ͜Ζ • HttpStackʹΑΓɺ௿ϨϕϧHttp ClientΛ੾Γସ͑Մೳ • Apache HTTP Client, HttpURLConnection (Hurl),

    OkHttp (via okhttp-urlconnection) • RequestQueueΫϥεʹΑΓɺHttpStackʹ౉͢લͷϦΫΤε τΩϡʔΛࡉ੍͔͘ޚՄೳ • ΫοࣾͷෳࡶͳೝূγεςϜΛ௨աͤ͞Δͷʹ͜Ε͕ඞཁ ͩͬͨͨΊ࠾༻ • ͞·͟·ͳίϯϙʔωϯτ͕֦ுՄೳ
  15. Volleyͷμϝͳͱ͜Ζ • Ϋϥεଟ͗͢ɺ࣮૷ෳࡶ͗͢ɺ֦ு͕೉͗͢͠ • ϦΫΤετΛΦϯϝϞϦͰॲཧ͢ΔઃܭͳͨΊɺ ը૾ΞοϓϩʔυΛ͠Α͏ͱ͢Δͱॲཧ͕ෳࡶ ͰόοΫΤϯυͷHttp Clientʹґଘ͕ͪ͠ • ࠓ΍deprecatedͱͳͬͨApache

    HTTP Client ͷߏ଄ମ܈ͱີ݁߹͍ͯͯ͠੾Γ཭͕͠ࠔ೉
  16. Retrofit • Squareࣾ੡ͷߴϨϕϧHttp Client • interfaceΛఆٛ͢Δ͚ͩͰWeb Api ClientΛ ࡞੒Ͱ͖Δ •

    RxJavaΠϯλʔϑΣΠε΋݉Ͷඋ͍͑ͯΔ
  17. Retrofitͷ͍͍ͱ͜Ζ • ద౰ͳσϑΥϧτͰΑ͠ͳʹಈ͘ • interfaceΛఆٛ͢Δ͚ͩͰWeb Api ClientΛੜ੒Ͱ͖ Δ • ͔͠΋RxJavaΠϯλʔϑΣΠε΋͋Δ

    • όοΫΤϯυͷ௿ϨϕϧHttp ClientΛ੾Γସ͑ΒΕΔ • ΠϯλʔϑΣΠε͸ඇৗʹ୯७Ͱ࣮૷͠΍͍͢
  18. Retrofitͷμϝͳͱ͜Ζ • Web Api ClientΛΞϊςʔγϣϯͱϦϑϨΫγϣϯͰ੍ޚ͍ͯ͠ΔͨΊ ࣮૷Λ௥͍ʹ͍͘ • ͍ͣΕ੩తίʔυੜ੒ܕʹͳΕ͹͋Δఔ౓͸ղফ͢Δ͔΋ • “Request”ͷΑ͏ͳ൚༻తͳ໊લΛ࢖͍͗͢ʢOkHttpͱ΋ඃͬͯΔʣ

    • σϑΥϧτ͸ʮݡ͍ʯ͕ద੾ͱ͸ݶΒͳ͍ • OkHttp΍RxJava͕ར༻Մೳ͔Ͳ͏͔ͰৼΔ෣͍͕มΘΔ • ϦΫΤετΛॲཧ͢ΔσϑΥϧτͷExecutor͕cached thread pool executorsͳͷͰεϨουΛ࡞Γ͗͢ΔՄೳੑ͕͋Δ
  19. ཧ૝తͳߴϨϕϧHttp Client • ʮݡ͍σϑΥϧτʯ͸ෆཁ • ͋ΔϥΠϒϥϦ͕ೖ͍ͬͯΔͱৼΔ෣͍͕มΘΔͳͲ͸࠷ѱ • Android͸Ϧιʔε͕গͳ͍͜ͱʹཹҙ͢Δ • εϨουΛͲ͏࢖͏͔ɺΞϓϦ͔Β੍ޚͰ͖Δ͔Ͳ͏͔͸ௐ

    ΂ͨ΄͏͕Α͍ • ϦΫΤετͱϨεϙϯεͷஔ׵͕Ͱ͖Δ͔Ͳ͏͔ • ෳࡶͳೝূΛ௨աͰ͖Δ͔Ͳ͏͔
  20. Thread Pool

  21. εϨουϓʔϧ͸ͳͥॏཁ͔ • HTTPϦΫΤετΛฒྻͰ࣮ߦ͢ΔͨΊʹ࢖͏ • 1ͭͷը໘Λ࡞ΔͨΊʹෳ਺ͷϦΫΤετ͕ඞ ཁͳ৔߹ɺ௚ྻ࣮ߦͩͱ1ͭͷϦΫΤετͷॲ ཧʹֻ͕͔࣌ؒͬͯΞϓϦશମ͕஗͘ͳΔ • ฒྻʹϦΫΤετΛ࣮ߦ͢Δͱɺ஗͍ϦΫΤ ετʹҾ͖ͣΒΕΔ౓߹͍͕খ͘͞ͳΔ

  22. εϨουϓʔϧʹ͍ͭͯ • AndroidͰ࡞ΕΔThread͸1000~10000ݸఔ౓ • ্ݶʹୡ͢ΔͱΞϓϦ͕ٸࢮ͢Δࣄ͕͋ΔͷͰ new Thread() ͸ةݥ • ͦ͜ͰεϨουϓʔϧͰεϨουΛ࢖͍·Θ͢

    • Web Api Client͸ฒྻϦΫΤετͷͨΊʹεϨο υϓʔϧΛ࢖͏
  23. ϦΫΤετ 1SJPSJUZ#MPDLJOH2VFVF5 5ISFBE 5ISFBE 5ISFBE Ϩεϙϯε 5ISFBE1PPM*NQMFNFOUBUJPO

  24. Thread PoolͷͦΕͧΕͷ࣮૷ • OkHttp - ೚ҙͰඪ४Cached Thread Pool • Retrofit

    - ඪ४Cached Thread Pool • Volley - ಠ࣮ࣗ૷ͷFixed Thread Pool • Picasso - ඪ४Fixed Thread Pool • ੍ޚՄೳ͕࣮ͩ૷ʹΑͬͯσϑΥϧτ஋͕ҧ͏ͷͰ஫ ҙ
  25. java.util.concurrent.Executors • εϨουϓʔϧΛੜ੒͢Δඪ४ΫϥεϥΠϒϥϦ • newCachedThreadPool() - ඞཁͳ͚ͩεϨου Λ࡞Γ࠶ར༻͢ΔεϨουϓʔϧʢεϨου਺ ͷ্ݶͳ͠ͳͷͰةݥʣ •

    newFixedThreadPool() - εϨου਺ݻఆͷε Ϩουϓʔϧ
  26. Thread Pool ϕετϓϥΫςΟε • ௿ϨϕϧHttp Client͸εϨουϓʔϧΛ࣋ͨͣɺߴ ϨϕϧHttp Client͕࣋ͭ • σϑΥϧτʹ͸པΒͣΞϓϦ͕executorΛੜ੒ͯ͠

    ౉͢ • εϨουΛ࡞Γա͗ͳ͍Fixed Thread PoolΛ࢖͏ • IOό΢ϯυͷ৔߹͸εϨου਺3͘Β͍ͰΑͦ͞͏
  27. ݁࿦ • ݱঢ়ͩͱOkHttp + Retrofit͕Αͦ͞͏ • RetrofitͷσϑΥϧτεϨουϓʔϧʹ஫ҙ • ͱ͸͍͑Retrofit΋ΘΓͱෆຬ͕͋ΔͷͰͲ͏ ͠Α͏͔ͳͱ͍͏ؾ࣋ͪ

  28. That’s all. • Any questions? • ͜ΜͳͷศརͩΑͱ͍͏࿩͕͋Ε͹ @__gfx__ ·Ͱڭ͍͑ͯͩ͘͞ʂʂʂ