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.7k
AssemblyScriptでライブラリコードの高速化をしてみる
gfx
5
2.9k
実践TypeScriptトークバトル
gfx
1
1.1k
歴史的経緯の説明 as code
gfx
7
2.7k
Elasticsearchによる 全文検索の実装 in Rails
gfx
5
9.3k
すばらしき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
Tuning GraphQL on Rails
pyama86
2
1k
Snowflake x dbtで作るセキュアでアジャイルなデータ基盤
tsoshiro
2
420
僕がつくった48個のWebサービス達
yusukebe
18
17k
Macとオーディオ再生 2024/11/02
yusukeito
0
140
qmuntal/stateless のススメ
sgash708
0
120
Googleのテストサイズを活用したテスト環境の構築
toms74209200
0
270
What’s New in Compose Multiplatform - A Live Tour (droidcon London 2024)
zsmb
1
330
『ドメイン駆動設計をはじめよう』のモデリングアプローチ
masuda220
PRO
7
420
[PyCon Korea 2024 Keynote] 커뮤니티와 파이썬, 그리고 우리
beomi
0
110
詳細解説! ArrayListの仕組みと実装
yujisoftware
0
470
Vue3の一歩踏み込んだパフォーマンスチューニング2024
hal_spidernight
3
3.1k
Universal Linksの実装方法と陥りがちな罠
kaitokudou
1
220
Featured
See All Featured
XXLCSS - How to scale CSS and keep your sanity
sugarenia
246
1.3M
Testing 201, or: Great Expectations
jmmastey
38
7k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
37
1.8k
Fashionably flexible responsive web design (full day workshop)
malarkey
404
65k
Code Reviewing Like a Champion
maltzj
519
39k
VelocityConf: Rendering Performance Case Studies
addyosmani
325
24k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
328
21k
The Power of CSS Pseudo Elements
geoffreycrofte
72
5.3k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
48k
Gamification - CAS2011
davidbonilla
80
5k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
42
9.2k
Producing Creativity
orderedlist
PRO
341
39k
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__ ·Ͱڭ͍͑ͯͩ͘͞ʂʂʂ