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
3k
実践TypeScriptトークバトル
gfx
1
1.1k
歴史的経緯の説明 as code
gfx
7
2.7k
Elasticsearchによる 全文検索の実装 in Rails
gfx
6
9.4k
すばらしきGraphQLのSEKAIへようこそ
gfx
20
9.2k
マルチテナント・ウェブアプリケーションの実践
gfx
14
9.5k
How to choose the ORM on Android
gfx
1
4.2k
How Do We Get Along With Static Types
gfx
5
3.3k
Other Decks in Programming
See All in Programming
Zoneless Testing
rainerhahnekamp
0
120
たのしいparse.y
ydah
3
120
Mermaid x AST x 生成AI = コードとドキュメントの完全同期への道
shibuyamizuho
0
160
数十万行のプロジェクトを Scala 2から3に完全移行した
xuwei_k
0
260
From Translations to Multi Dimension Entities
alexanderschranz
2
130
今年のアップデートで振り返るCDKセキュリティのシフトレフト/2024-cdk-security-shift-left
tomoki10
0
190
Monixと常駐プログラムの勘どころ / Scalaわいわい勉強会 #4
stoneream
0
270
Итераторы в Go 1.23: зачем они нужны, как использовать, и насколько они быстрые?
lamodatech
0
650
LLM Supervised Fine-tuningの理論と実践
datanalyticslabo
3
960
コンテナをたくさん詰め込んだシステムとランタイムの変化
makihiro
1
120
ゆるやかにgolangci-lintのルールを強くする / Kyoto.go #56
utgwkk
1
360
14 Years of iOS: Lessons and Key Points
seyfoyun
1
770
Featured
See All Featured
Measuring & Analyzing Core Web Vitals
bluesmoon
4
170
Speed Design
sergeychernyshev
25
670
[RailsConf 2023] Rails as a piece of cake
palkan
53
5k
Fireside Chat
paigeccino
34
3.1k
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.4k
YesSQL, Process and Tooling at Scale
rocio
169
14k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
810
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
28
4.3k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
Producing Creativity
orderedlist
PRO
341
39k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
127
18k
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__ ·Ͱڭ͍͑ͯͩ͘͞ʂʂʂ