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

Web Api Clientを考える #dotsandroid

FUJI Goro
August 08, 2015

Web Api Clientを考える #dotsandroid

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

FUJI Goro

August 08, 2015
Tweet

More Decks by FUJI Goro

Other Decks in Programming

Transcript

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

    • ΫοΫύουΞϓϦͷϦϦʔε؅ཧɾϝϯ ςφϯεΛओʹ͍ͯ͠Δ • ࠲ӈͷ໏͸ʮਖ਼͍͠ίʔυΛॻ͜͏ɻಈ ͘ίʔυͰຬ଍ͤͣʹʯ
  2. ༻ޠ • 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ͱͷ۠ผ͸ᐆດͳ͜ͱ͕͋Δ
  3. Apache HTTP Client • Androidʹ૊Έࠐ·Εͨ௿ϨϕϧHttp Client • DEPRECATED! DEPRECATED! DEPRECATED!

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

    ΠϯλʔϑΣΠε͸HTTPΛӅṭ͍͗ͯͯ͢͠෼͔Γʹ͍͘ • Android 4.4Ҏ߱͸όοΫΤϯυ࣮૷͕OkHttpʹͳͬͨ ͕ɺ͜Ε͕ΩϟογϡपΓʹόά͕͋ΓΫϥογϡ͢Δͷ Ͱ࢖Θͳ͍΄͏͕ແ೉
  5. VolleyͷΑ͍ͱ͜Ζ • HttpStackʹΑΓɺ௿ϨϕϧHttp ClientΛ੾Γସ͑Մೳ • Apache HTTP Client, HttpURLConnection (Hurl),

    OkHttp (via okhttp-urlconnection) • RequestQueueΫϥεʹΑΓɺHttpStackʹ౉͢લͷϦΫΤε τΩϡʔΛࡉ੍͔͘ޚՄೳ • ΫοࣾͷෳࡶͳೝূγεςϜΛ௨աͤ͞Δͷʹ͜Ε͕ඞཁ ͩͬͨͨΊ࠾༻ • ͞·͟·ͳίϯϙʔωϯτ͕֦ுՄೳ
  6. Retrofitͷ͍͍ͱ͜Ζ • ద౰ͳσϑΥϧτͰΑ͠ͳʹಈ͘ • interfaceΛఆٛ͢Δ͚ͩͰWeb Api ClientΛੜ੒Ͱ͖ Δ • ͔͠΋RxJavaΠϯλʔϑΣΠε΋͋Δ

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

    • σϑΥϧτ͸ʮݡ͍ʯ͕ద੾ͱ͸ݶΒͳ͍ • OkHttp΍RxJava͕ར༻Մೳ͔Ͳ͏͔ͰৼΔ෣͍͕มΘΔ • ϦΫΤετΛॲཧ͢ΔσϑΥϧτͷExecutor͕cached thread pool executorsͳͷͰεϨουΛ࡞Γ͗͢ΔՄೳੑ͕͋Δ
  8. Thread PoolͷͦΕͧΕͷ࣮૷ • OkHttp - ೚ҙͰඪ४Cached Thread Pool • Retrofit

    - ඪ४Cached Thread Pool • Volley - ಠ࣮ࣗ૷ͷFixed Thread Pool • Picasso - ඪ४Fixed Thread Pool • ੍ޚՄೳ͕࣮ͩ૷ʹΑͬͯσϑΥϧτ஋͕ҧ͏ͷͰ஫ ҙ
  9. Thread Pool ϕετϓϥΫςΟε • ௿ϨϕϧHttp Client͸εϨουϓʔϧΛ࣋ͨͣɺߴ ϨϕϧHttp Client͕࣋ͭ • σϑΥϧτʹ͸པΒͣΞϓϦ͕executorΛੜ੒ͯ͠

    ౉͢ • εϨουΛ࡞Γա͗ͳ͍Fixed Thread PoolΛ࢖͏ • IOό΢ϯυͷ৔߹͸εϨου਺3͘Β͍ͰΑͦ͞͏