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
Streams APIとTCPフロー制御 / Web Streams API and TCP flow control
tasshi
1
290
Vue.js学習の振り返り
hiro_xre
2
130
Go言語でターミナルフレンドリーなAIコマンド、afaを作った/fukuokago20_afa
monochromegane
2
140
推し活の ハイトラフィックに立ち向かう Railsとアーキテクチャ - Kaigi on Rails 2024
falcon8823
6
2.2k
役立つログに取り組もう
irof
26
8.6k
Kubernetes for Data Engineers: Building Scalable, Reliable Data Pipelines
sucitw
1
200
WEBエンジニア向けAI活用入門
sutetotanuki
0
300
go.mod、DockerfileやCI設定に分散しがちなGoのバージョンをまとめて管理する / Go Connect #3
arthur1
10
2.4k
Sidekiqで実現する 長時間非同期処理の中断と再開 / Pausing and Resuming Long-Running Asynchronous Jobs with Sidekiq
hypermkt
6
2.7k
Macとオーディオ再生 2024/11/02
yusukeito
0
160
PagerDuty を軸にした On-Call 構築と運用課題の解決 / PagerDuty Japan Community Meetup 4
horimislime
1
110
破壊せよ!データ破壊駆動で考えるドメインモデリング / data-destroy-driven
minodriven
16
4k
Featured
See All Featured
GitHub's CSS Performance
jonrohan
1030
460k
Visualization
eitanlees
144
15k
Why You Should Never Use an ORM
jnunemaker
PRO
53
9k
Unsuck your backbone
ammeep
668
57k
Into the Great Unknown - MozCon
thekraken
31
1.5k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
48k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
37
1.8k
A better future with KSS
kneath
238
17k
VelocityConf: Rendering Performance Case Studies
addyosmani
325
24k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
27
4.2k
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__ ·Ͱڭ͍͑ͯͩ͘͞ʂʂʂ