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
TypeScript Graph でコードレビューの心理的障壁を乗り越える
ysk8hori
2
1.1k
Pinia Colada が実現するスマートな非同期処理
naokihaba
4
220
Generative AI Use Cases JP (略称:GenU)奮闘記
hideg
1
290
Click-free releases & the making of a CLI app
oheyadam
2
110
Make Impossible States Impossibleを 意識してReactのPropsを設計しよう
ikumatadokoro
0
170
OnlineTestConf: Test Automation Friend or Foe
maaretp
0
110
最新TCAキャッチアップ
0si43
0
140
Quine, Polyglot, 良いコード
qnighy
4
640
Arm移行タイムアタック
qnighy
0
310
Realtime API 入門
riofujimon
0
150
OSSで起業してもうすぐ10年 / Open Source Conference 2024 Shimane
furukawayasuto
0
100
Kaigi on Rails 2024 〜運営の裏側〜
krpk1900
1
190
Featured
See All Featured
Fireside Chat
paigeccino
34
3k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.7k
Happy Clients
brianwarren
98
6.7k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
506
140k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
27
840
The Art of Programming - Codeland 2020
erikaheidi
52
13k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
25
1.8k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
47
2.1k
Writing Fast Ruby
sferik
627
61k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
131
33k
We Have a Design System, Now What?
morganepeng
50
7.2k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
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__ ·Ͱڭ͍͑ͯͩ͘͞ʂʂʂ