Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Web Api Clientを考える #dotsandroid
Search
FUJI Goro
August 08, 2015
Programming
11
6.8k
Web Api Clientを考える #dotsandroid
Androidオールスターズ 2015/8/8 の資料です
FUJI Goro
August 08, 2015
Tweet
Share
More Decks by FUJI Goro
See All by FUJI Goro
How to Boost Your Code with WebAssembly
gfx
2
2.8k
AssemblyScriptでライブラリコードの高速化をしてみる
gfx
5
2.9k
実践TypeScriptトークバトル
gfx
1
1.1k
歴史的経緯の説明 as code
gfx
7
2.7k
Elasticsearchによる 全文検索の実装 in Rails
gfx
5
9.4k
すばらしきGraphQLのSEKAIへようこそ
gfx
20
9.2k
マルチテナント・ウェブアプリケーションの実践
gfx
14
9.4k
How to choose the ORM on Android
gfx
1
4.1k
How Do We Get Along With Static Types
gfx
5
3.3k
Other Decks in Programming
See All in Programming
ActiveSupport::Notifications supporting instrumentation of Rails apps with OpenTelemetry
ymtdzzz
1
230
macOS でできる リアルタイム動画像処理
biacco42
9
2.4k
Remix on Hono on Cloudflare Workers
yusukebe
1
290
ECS Service Connectのこれまでのアップデートと今後のRoadmapを見てみる
tkikuc
2
250
リアーキテクチャxDDD 1年間の取り組みと進化
hsawaji
1
220
as(型アサーション)を書く前にできること
marokanatani
10
2.7k
Click-free releases & the making of a CLI app
oheyadam
2
120
Snowflake x dbtで作るセキュアでアジャイルなデータ基盤
tsoshiro
2
520
Realtime API 入門
riofujimon
0
150
ローコードSaaSのUXを向上させるためのTypeScript
taro28
1
620
C++でシェーダを書く
fadis
6
4.1k
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
1
100
Featured
See All Featured
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
The Art of Programming - Codeland 2020
erikaheidi
52
13k
A Modern Web Designer's Workflow
chriscoyier
693
190k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
6
410
Automating Front-end Workflow
addyosmani
1366
200k
Fireside Chat
paigeccino
34
3k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.1k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
4
370
RailsConf 2023
tenderlove
29
900
Building Adaptive Systems
keathley
38
2.3k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
131
33k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
Transcript
Web Api ClientΛߟ͑Δ AndroidΦʔϧελʔζ 2015/8/8 #dotsandroid @__gfx__ at Cookpad, Inc.
Introduction to myself • github:gfx, twitter:__gfx__, FUJI Goro • ΫοΫύουॴଐ
• ΫοΫύουΞϓϦͷϦϦʔεཧɾϝϯ ςφϯεΛओʹ͍ͯ͠Δ • ࠲ӈͷʮਖ਼͍͠ίʔυΛॻ͜͏ɻಈ ͘ίʔυͰຬͤͣʹʯ
ϥΠϒϥϦͷબఆํ • Android൛ΫοΫύουΞϓϦͰ࠾༻͍ͯ͠Δٕज़ͷݱ ঢ়֬ೝ http://techlife.cookpad.com/entry/2015/06/25/ 093507 • “ΫοΫύουͷΣϒαʔϏεಉ༷ʹɺϞόΠϧΞϓ Ϧͷ։ൃ͜ͷઌ10Ҏ্ଓ͘Ͱ͠ΐ͏ɻ͕ٕͨͬͯ͠ ज़બͷجຊతͳࢦʹ͓͍ͯɺ͜ͷઌ10ؒܧଓ͠
ͯϝϯςφϯε͓Αͼߋ৽͍ͯ͘͜͠ͱ͕Ͱ͖Δ͔Ͳ͏ ͔͕ॏཁͰ͢ɻ”
Web Api Client
ͳͥWeb Api Client͕ॏཁͳͷ͔ • ଟ͘ͷεϚϗΞϓϦɺWeb APIͷΠϯλʔϑΣΠ εʹ͗͢ͳ͍ • Cookpadྫ֎Ͱͳ͘ɺΞϓϦجຊతʹ JSONΛฦ͢Web
APIͷΠϯλʔϑΣΠεͰ͋ Δ • Ώ͑ʹWeb Api ClientͰ͏Http ClientΛԿʹ͢ Δ͔ඇৗʹॏཁ
༻ޠ • 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ͱͷ۠ผᐆດͳ͜ͱ͕͋Δ
ϨϕϧHttp Clientੇධ
Apache HTTP Client • AndroidʹΈࠐ·ΕͨϨϕϧHttp Client • DEPRECATED! DEPRECATED! DEPRECATED!
• ΈࠐΈ൛ͱϦϦʔε൛ͷဃ͕େ͖͍ • multipart requestͳͲมΘͬͨ͜ͱΛ͢ΔʹΈࠐΈ൛Ͱ ΓͣɺదͳόʔδϣϯͷϦϦʔε൛ίϯ ϙʔωϯτΛ͏ඞཁ͕͋Δ • ͳͥAndroid frameworkʹΈࠐΜͩͷͩ…
HttpURLConnection • Javaඪ४ΫϥεϥΠϒϥϦͷHttp Client • యܕతͳ͍ํͰΩϟετ͕ඞཁͱ͍͏ҟৗͳઃܭ • (HttpURLConnection) url.openConnection(); •
ΠϯλʔϑΣΠεHTTPΛӅṭ͍͔͗ͯͯ͢͠Γʹ͍͘ • Android 4.4Ҏ߱όοΫΤϯυ࣮͕OkHttpʹͳͬͨ ͕ɺ͜Ε͕ΩϟογϡपΓʹόά͕͋ΓΫϥογϡ͢Δͷ ͰΘͳ͍΄͏͕ແ
OkHttp • Squareࣾ։ൃͷϨϕϧHttp Client • HTTP/2αϙʔτ • ಉظAPIͱඇಉظAPI྆ํ͋Δ • ͳͥ྆ํ༻ҙͨ͠͠…͜Εͷ͍ͤͰʢ͈́
• AndroidΈࠐΈͰͳ͍ͷ͕େ͖ͳϝϦοτ • ࣮ߦ͞ΕΔίʔυΛશʹ༧ଌͰ͖Δʂʂʂ
ཧతͳϨϕϧHttp Client • ϚϧνεϨουΛѻΘͳ͍͜ͱ • ฒྻϦΫΤετͳͲΑΓ্ҐϨΠϠͰΔ ͖ • ͨͩ͠εϨουηʔϑͰ͋Δ͖ •
pure JavaͰ࣮͠ɺAndroid frameworkͷ Http Clientʹґଘ͠ͳ͍͜ͱ
ߴϨϕϧHttp Clientੇධ
Volley • 2013ͷGoogle I/O ʹͯክͷΑ͏ʹݱΕ ͨߴϨϕϧHttp Client • AndroidքͷελϯμʔυʹͳΔ͔ͱࢥΘΕͨ ͕…
VolleyͷΑ͍ͱ͜Ζ • HttpStackʹΑΓɺϨϕϧHttp ClientΛΓସ͑Մೳ • Apache HTTP Client, HttpURLConnection (Hurl),
OkHttp (via okhttp-urlconnection) • RequestQueueΫϥεʹΑΓɺHttpStackʹ͢લͷϦΫΤε τΩϡʔΛࡉ੍͔͘ޚՄೳ • ΫοࣾͷෳࡶͳೝূγεςϜΛ௨աͤ͞Δͷʹ͜Ε͕ඞཁ ͩͬͨͨΊ࠾༻ • ͞·͟·ͳίϯϙʔωϯτ͕֦ுՄೳ
Volleyͷμϝͳͱ͜Ζ • Ϋϥεଟ͗͢ɺ࣮ෳࡶ͗͢ɺ֦ு͕͗͢͠ • ϦΫΤετΛΦϯϝϞϦͰॲཧ͢ΔઃܭͳͨΊɺ ը૾ΞοϓϩʔυΛ͠Α͏ͱ͢Δͱॲཧ͕ෳࡶ ͰόοΫΤϯυͷHttp Clientʹґଘ͕ͪ͠ • ࠓdeprecatedͱͳͬͨApache
HTTP Client ͷߏମ܈ͱີ݁߹͍ͯͯ͠Γ͕͠ࠔ
Retrofit • SquareࣾͷߴϨϕϧHttp Client • interfaceΛఆٛ͢Δ͚ͩͰWeb Api ClientΛ ࡞Ͱ͖Δ •
RxJavaΠϯλʔϑΣΠε݉Ͷඋ͍͑ͯΔ
Retrofitͷ͍͍ͱ͜Ζ • దͳσϑΥϧτͰΑ͠ͳʹಈ͘ • interfaceΛఆٛ͢Δ͚ͩͰWeb Api ClientΛੜͰ͖ Δ • ͔͠RxJavaΠϯλʔϑΣΠε͋Δ
• όοΫΤϯυͷϨϕϧHttp ClientΛΓସ͑ΒΕΔ • ΠϯλʔϑΣΠεඇৗʹ୯७Ͱ࣮͍͢͠
Retrofitͷμϝͳͱ͜Ζ • Web Api ClientΛΞϊςʔγϣϯͱϦϑϨΫγϣϯͰ੍ޚ͍ͯ͠ΔͨΊ ࣮Λ͍ʹ͍͘ • ͍ͣΕ੩తίʔυੜܕʹͳΕ͋Δఔղফ͢Δ͔ • “Request”ͷΑ͏ͳ൚༻తͳ໊લΛ͍͗͢ʢOkHttpͱඃͬͯΔʣ
• σϑΥϧτʮݡ͍ʯ͕దͱݶΒͳ͍ • OkHttpRxJava͕ར༻Մೳ͔Ͳ͏͔ͰৼΔ͍͕มΘΔ • ϦΫΤετΛॲཧ͢ΔσϑΥϧτͷExecutor͕cached thread pool executorsͳͷͰεϨουΛ࡞Γ͗͢ΔՄೳੑ͕͋Δ
ཧతͳߴϨϕϧHttp Client • ʮݡ͍σϑΥϧτʯෆཁ • ͋ΔϥΠϒϥϦ͕ೖ͍ͬͯΔͱৼΔ͍͕มΘΔͳͲ࠷ѱ • AndroidϦιʔε͕গͳ͍͜ͱʹཹҙ͢Δ • εϨουΛͲ͏͏͔ɺΞϓϦ͔Β੍ޚͰ͖Δ͔Ͳ͏͔ௐ
ͨ΄͏͕Α͍ • ϦΫΤετͱϨεϙϯεͷஔ͕Ͱ͖Δ͔Ͳ͏͔ • ෳࡶͳೝূΛ௨աͰ͖Δ͔Ͳ͏͔
Thread Pool
εϨουϓʔϧͳͥॏཁ͔ • HTTPϦΫΤετΛฒྻͰ࣮ߦ͢ΔͨΊʹ͏ • 1ͭͷը໘Λ࡞ΔͨΊʹෳͷϦΫΤετ͕ඞ ཁͳ߹ɺྻ࣮ߦͩͱ1ͭͷϦΫΤετͷॲ ཧʹֻ͕͔࣌ؒͬͯΞϓϦશମ͕͘ͳΔ • ฒྻʹϦΫΤετΛ࣮ߦ͢Δͱɺ͍ϦΫΤ ετʹҾ͖ͣΒΕΔ߹͍͕খ͘͞ͳΔ
εϨουϓʔϧʹ͍ͭͯ • AndroidͰ࡞ΕΔThread1000~10000ݸఔ • ্ݶʹୡ͢ΔͱΞϓϦ͕ٸࢮ͢Δࣄ͕͋ΔͷͰ new Thread() ةݥ • ͦ͜ͰεϨουϓʔϧͰεϨουΛ͍·Θ͢
• Web Api ClientฒྻϦΫΤετͷͨΊʹεϨο υϓʔϧΛ͏
ϦΫΤετ 1SJPSJUZ#MPDLJOH2VFVF5 5ISFBE 5ISFBE 5ISFBE Ϩεϙϯε 5ISFBE1PPM*NQMFNFOUBUJPO
Thread PoolͷͦΕͧΕͷ࣮ • OkHttp - ҙͰඪ४Cached Thread Pool • Retrofit
- ඪ४Cached Thread Pool • Volley - ಠ࣮ࣗͷFixed Thread Pool • Picasso - ඪ४Fixed Thread Pool • ੍ޚՄೳ͕࣮ͩʹΑͬͯσϑΥϧτ͕ҧ͏ͷͰ ҙ
java.util.concurrent.Executors • εϨουϓʔϧΛੜ͢Δඪ४ΫϥεϥΠϒϥϦ • newCachedThreadPool() - ඞཁͳ͚ͩεϨου Λ࡞Γ࠶ར༻͢ΔεϨουϓʔϧʢεϨου ͷ্ݶͳ͠ͳͷͰةݥʣ •
newFixedThreadPool() - εϨουݻఆͷε Ϩουϓʔϧ
Thread Pool ϕετϓϥΫςΟε • ϨϕϧHttp ClientεϨουϓʔϧΛ࣋ͨͣɺߴ ϨϕϧHttp Client͕࣋ͭ • σϑΥϧτʹཔΒͣΞϓϦ͕executorΛੜͯ͠
͢ • εϨουΛ࡞Γա͗ͳ͍Fixed Thread PoolΛ͏ • IOόϯυͷ߹εϨου3͘Β͍ͰΑͦ͞͏
݁ • ݱঢ়ͩͱOkHttp + Retrofit͕Αͦ͞͏ • RetrofitͷσϑΥϧτεϨουϓʔϧʹҙ • ͱ͍͑RetrofitΘΓͱෆຬ͕͋ΔͷͰͲ͏ ͠Α͏͔ͳͱ͍͏ؾ࣋ͪ
That’s all. • Any questions? • ͜ΜͳͷศརͩΑͱ͍͏͕͋Ε @__gfx__ ·Ͱڭ͍͑ͯͩ͘͞ʂʂʂ