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. 2.

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

    • ΫοΫύουΞϓϦͷϦϦʔε؅ཧɾϝϯ ςφϯεΛओʹ͍ͯ͠Δ • ࠲ӈͷ໏͸ʮਖ਼͍͠ίʔυΛॻ͜͏ɻಈ ͘ίʔυͰຬ଍ͤͣʹʯ
  2. 5.
  3. 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ͱͷ۠ผ͸ᐆດͳ͜ͱ͕͋Δ
  4. 8.

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

    • ૊ΈࠐΈ൛ͱϦϦʔε൛ͷဃ཭͕େ͖͍ • multipart requestͳͲมΘͬͨ͜ͱΛ͢Δʹ͸૊ΈࠐΈ൛Ͱ͸଍ Γͣɺద੾ͳόʔδϣϯͷϦϦʔε൛ίϯ ϙʔωϯτΛ࢖͏ඞཁ͕͋Δ • ͳͥAndroid frameworkʹ૊ΈࠐΜͩͷͩ…
  5. 9.

    HttpURLConnection • Javaඪ४ΫϥεϥΠϒϥϦͷHttp Client • యܕతͳ࢖͍ํͰΩϟετ͕ඞཁͱ͍͏ҟৗͳઃܭ • (HttpURLConnection) url.openConnection(); •

    ΠϯλʔϑΣΠε͸HTTPΛӅṭ͍͗ͯͯ͢͠෼͔Γʹ͍͘ • Android 4.4Ҏ߱͸όοΫΤϯυ࣮૷͕OkHttpʹͳͬͨ ͕ɺ͜Ε͕ΩϟογϡपΓʹόά͕͋ΓΫϥογϡ͢Δͷ Ͱ࢖Θͳ͍΄͏͕ແ೉
  6. 10.
  7. 14.

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

    OkHttp (via okhttp-urlconnection) • RequestQueueΫϥεʹΑΓɺHttpStackʹ౉͢લͷϦΫΤε τΩϡʔΛࡉ੍͔͘ޚՄೳ • ΫοࣾͷෳࡶͳೝূγεςϜΛ௨աͤ͞Δͷʹ͜Ε͕ඞཁ ͩͬͨͨΊ࠾༻ • ͞·͟·ͳίϯϙʔωϯτ͕֦ுՄೳ
  8. 17.

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

    • όοΫΤϯυͷ௿ϨϕϧHttp ClientΛ੾Γସ͑ΒΕΔ • ΠϯλʔϑΣΠε͸ඇৗʹ୯७Ͱ࣮૷͠΍͍͢
  9. 18.

    Retrofitͷμϝͳͱ͜Ζ • Web Api ClientΛΞϊςʔγϣϯͱϦϑϨΫγϣϯͰ੍ޚ͍ͯ͠ΔͨΊ ࣮૷Λ௥͍ʹ͍͘ • ͍ͣΕ੩తίʔυੜ੒ܕʹͳΕ͹͋Δఔ౓͸ղফ͢Δ͔΋ • “Request”ͷΑ͏ͳ൚༻తͳ໊લΛ࢖͍͗͢ʢOkHttpͱ΋ඃͬͯΔʣ

    • σϑΥϧτ͸ʮݡ͍ʯ͕ద੾ͱ͸ݶΒͳ͍ • OkHttp΍RxJava͕ར༻Մೳ͔Ͳ͏͔ͰৼΔ෣͍͕มΘΔ • ϦΫΤετΛॲཧ͢ΔσϑΥϧτͷExecutor͕cached thread pool executorsͳͷͰεϨουΛ࡞Γ͗͢ΔՄೳੑ͕͋Δ
  10. 24.

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

    - ඪ४Cached Thread Pool • Volley - ಠ࣮ࣗ૷ͷFixed Thread Pool • Picasso - ඪ४Fixed Thread Pool • ੍ޚՄೳ͕࣮ͩ૷ʹΑͬͯσϑΥϧτ஋͕ҧ͏ͷͰ஫ ҙ
  11. 26.

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

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