Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
2017/03/25 RxJava2 + OkHttp + Retrofit入門
Search
shige0501
March 25, 2017
Programming
1.9k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
2017/03/25 RxJava2 + OkHttp + Retrofit入門
日本Androidの会 中国支部 第34回勉強会の資料です
https://cjag.connpass.com/event/51822/
shige0501
March 25, 2017
More Decks by shige0501
See All by shige0501
グロースするプロダクトの開発プロセスと関わり方
shige0501
0
360
モバイルアプリを効率的に開発するクロスプラットフォーム開発という選択肢
shige0501
1
440
Other Decks in Programming
See All in Programming
Building an Out-of-Order CPU
latte72
1
780
テストを司るデーモンに会いに行く 〜隔離した仮想マシンでテストを通すまで〜
h1d3mun3
1
430
The Good Stuff, Not the Slop: Engineering High-Quality Android Apps with Modern AI Tooling
danybony
1
250
20260914 AIエージェント時代のPlatform Engineering LLM基盤とプロダクトの責務境界線
kanfab1
7
2k
WebMCP Challenge に星空観察アプリで参加した話
okajun35
0
180
How I Stole PSI from Android Studio - DroidKaigi2026
worker8
0
130
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
410
C#の現在地 進化の歴史と、AI時代の.NET Everywhere
neuecc
4
2.1k
数年滞っていたダークモード対応をおよそ2週間で完了させる
chigichan24
0
720
AWS DevOps Agentで インシデント対応をAIに任せたい
honmarkhunt
7
3k
wkhtmltopdfの次どうするか問題2026
willnet
2
1.2k
Omarchy Tokyo やると聞いて UMPC 買ってセットアップしてきた
mtsmfm
0
140
Featured
See All Featured
What's in a price? How to price your products and services
michaelherold
247
13k
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
240
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
260
Writing Fast Ruby
sferik
630
63k
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
67
58k
The Curse of the Amulet
leimatthew05
3
14k
Done Done
chrislema
186
16k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
4.8k
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
Are puppies a ranking factor?
jonoalderson
2
3.9k
Sam Torres - BigQuery for SEOs
techseoconnect
PRO
0
540
Transcript
RxJava2 + OkHttp + Retrofit ೖ 2017/03/25 ຊAndroidͷձ தࠃࢧ෦ ॏଜ
ߒೋ
ࣗݾհ • ໊લ • ॏଜ ߒೋ (Koji Shigemura) • ॴଐ
• ຊAndroidͷձ ίϛϡχςΟӡӦҕһ தࠃࢧ෦ • ChatWorkגࣜձࣾ ϞόΠϧɾιϑτΣΞΤϯδχΞ (Android) http://chatwork.com/
ධൃചதʂ Android SDKϙέοτϦϑΝϨϯε https://www.amazon.co.jp/dp/477416335X
ΞδΣϯμ • RxJava2 ֓ཁ • OkHttp ֓ཁ • Retrofit ֓ཁ
• Έ߹Θ࣮ͤͨ
RxJava2 ֓ཁ
RxJava ͱʁ • Microsoft ͷ .NET Framework ͷ࣮ݧతϥΠϒϥϦͱͯ͠ 2009ʹੜ·ΕͨʮReactive Extensionsʯ͕2012ʹ
ΦʔϓϯιʔεԽ ReactiveX: http://reactivex.io/ • ޙʹ Netflix ͕ Java ʹҠ২͠ɺΦʔϓϯιʔεԽͨ͠ͷ͕ RxJava RxJava: https://github.com/ReactiveX/RxJava • ϦΞΫςΟϒϓϩάϥϛϯά͕࣮ફ͍͢͠
RxJava2 • Reactive Streams ʹ४ڌ Reactive Streams: http://www.reactive-streams.org/ • RxJava
1.x Reactive Streams ͷ༷͕ग़ͯདྷΔલ͔ ΒϦϦʔε͞Ε͍ͯΔͷͰɺରԠͰ͖͍ͯͳ͍ ࠓ RxJava 1.xܥʹ৮Ε·ͤΜ
ྫ (ࣈͷϦετΛॲཧ͢Δ) private static final List<Integer> numList = Arrays.asList(1, 2,
3, 4, 5, 6, 7, 8, 9, 10); public static List<Integer> getEvenListsBefore() { List<Integer> evenLists = new ArrayList<>(); for (Integer num : numList) { if (num % 2 == 0) { evenLists.add(num); } } return evenLists; } // ݺͼग़͠ݩ Log.d("hogehoge", RxSample.getEvenListBefore().toString()); // ࣮ߦ݁Ռ [2, 4, 6, 8, 10] Before
ྫ (ࣈͷϦετΛॲཧ͢Δ) private static final List<Integer> numList = Arrays.asList(1, 2,
3, 4, 5, 6, 7, 8, 9, 10); public static List<Integer> getEvenListsBefore() { List<Integer> evenLists = new ArrayList<>(); for (Integer num : numList) { if (num % 2 == 0) { evenLists.add(num); } } return evenLists; } // ݺͼग़͠ݩ Log.d("hogehoge", RxSample.getEvenListBefore().toString()); // ࣮ߦ݁Ռ [2, 4, 6, 8, 10] Before ListͰ1 ~ 10ͷྻΛఆٛ
ྫ (ࣈͷϦετΛॲཧ͢Δ) private static final List<Integer> numList = Arrays.asList(1, 2,
3, 4, 5, 6, 7, 8, 9, 10); public static List<Integer> getEvenListsBefore() { List<Integer> evenLists = new ArrayList<>(); for (Integer num : numList) { if (num % 2 == 0) { evenLists.add(num); } } return evenLists; } // ݺͼग़͠ݩ Log.d("hogehoge", RxSample.getEvenListBefore().toString()); // ࣮ߦ݁Ռ [2, 4, 6, 8, 10] Before ۮͷϦετΛ࡞Γɺฦ͢
ྫ (ࣈͷϦετΛॲཧ͢Δ) private static final List<Integer> numList = Arrays.asList(1, 2,
3, 4, 5, 6, 7, 8, 9, 10); public static List<Integer> getEvenListsBefore() { List<Integer> evenLists = new ArrayList<>(); for (Integer num : numList) { if (num % 2 == 0) { evenLists.add(num); } } return evenLists; } // ݺͼग़͠ݩ Log.d("hogehoge", RxSample.getEvenListBefore().toString()); // ࣮ߦ݁Ռ [2, 4, 6, 8, 10] Before ۮͷϦετΛ toString() Ͱจࣈྻʹͯ͠ग़ྗ
ྫ (ࣈͷϦετΛॲཧ͢Δ) public static Single<List<Integer>> getEvenListAfter() { return Flowable.just(1, 2,
3, 4, 5, 6, 7, 8, 9, 10) .filter(i -> i % 2 == 0) .toList(); } // ݺͼग़͠ݩ Log.d("hogehoge", RxSample.getEvenListAfter().blockingGet().toString()); // ࣮ߦ݁Ռ [2, 4, 6, 8, 10] After
ྫ (ࣈͷϦετΛॲཧ͢Δ) public static Single<List<Integer>> getEvenListAfter() { return Flowable.just(1, 2,
3, 4, 5, 6, 7, 8, 9, 10) .filter(i -> i % 2 == 0) .toList(); } // ݺͼग़͠ݩ Log.d("hogehoge", RxSample.getEvenListAfter().blockingGet().toString()); // ࣮ߦ݁Ռ [2, 4, 6, 8, 10] After Flowable.just()ʹ͞ΕͨҾΛ1ͭͣͭྲྀ͢ ʢ1 -> 2 -> 3ͱॱ൪ʹ࣍ͷϝιουʹྲྀΕΔʣ
ྫ (ࣈͷϦετΛॲཧ͢Δ) public static Single<List<Integer>> getEvenListAfter() { return Flowable.just(1, 2,
3, 4, 5, 6, 7, 8, 9, 10) .filter(i -> i % 2 == 0) .toList(); } // ݺͼग़͠ݩ Log.d("hogehoge", RxSample.getEvenListAfter().blockingGet().toString()); // ࣮ߦ݁Ռ [2, 4, 6, 8, 10] After ͞Ε͖ͯͨͰɺۮͷͷ (i % 2 == 0 ͕ true) ͷͷΛϦετͱͯ͠ɺΓͱͯ͠ฦ͢
ྫ (ࣈͷϦετΛॲཧ͢Δ) public static Single<List<Integer>> getEvenListAfter() { return Flowable.just(1, 2,
3, 4, 5, 6, 7, 8, 9, 10) .filter(i -> i % 2 == 0) .toList(); } // ݺͼग़͠ݩ Log.d("hogehoge", RxSample.getEvenListAfter().blockingGet().toString()); // ࣮ߦ݁Ռ [2, 4, 6, 8, 10] After getEvenListAfter()ͷ݁ՌΛ.blockingGet()ͰΛऔ Γग़͠ɺ.toString()ͰதΛग़ྗ
OkHttp ֓ཁ
OkHttp ͱʁ • Square ͕ࣾఏڙ͢Δ HTTP௨৴ ͱ SPDY௨৴͢ΔͨΊ ͷΦʔϓϯιʔεϥΠϒϥϦ OkHttp:
http://square.github.io/okhttp/
ͳͥ OkHttp ͳͷ͔ʁ • Android SDK ʹΈࠐ·Ε͍ͯͨ Apache HTTP Client
͕ Android 6.0 (Marshmallow) Ͱ Deprecatedʹ • ҆ఆͯ͑͠Δ௨৴ΫϥΠΞϯτͱͯ͠ɺOkHttp͕ݱঢ় ͷσϑΝΫτελϯμʔυ
OkHttpͷྫলུ ʢࠓճͷϝΠϯRetrofitͳͷͰʣ
Retrofit֓ཁ
Retrofitͱʁ • Square ͕ࣾ։ൃͨ͠ܕ҆શͳHTTP Client Retrofit: http://square.github.io/retrofit/ • REST௨৴͕؆ܿԽͰ͖ɺPOJOมͯ͘͠ΕΔ •
๛ͳίϯόʔλ • GSON, Jackson, Moshi, Protobuf….. and more.
Έ߹Θ࣮ͤͨ
ࠓճͷαϯϓϧ༷ • Connpass͔ΒࠓճͷษڧձͷใΛऔಘ͠ɺը໘ʹද ࣔ͢Δ • ConnpassͷAPI༷: https://connpass.com/about/api/ • αϯϓϧϓϩδΣΫτ:
https://github.com/shige0501/retrofit_sample
࣮ߦ݁Ռ
લఏ • Retrolambda Λಋೖ͠ɺϥϜμࣜͰ࣮͍ͯ͠·͢ • αϯϓϧͷՄಡੑΛҙࣝͯ͠ɺ͋͑ͯจࣈྻΛϦιʔε Խ͍ͯ͠·ͤΜ
ϥΠϒϥϦͷಋೖ dependencies { : // RxJava2 compile "io.reactivex.rxjava2:rxjava:${rxjava2_version}" compile "io.reactivex.rxjava2:rxandroid:${rxandroid2_version}"
// Utils compile "com.google.code.gson:gson:${gson_version}" // Square compile “com.squareup.okhttp3:okhttp:${okhttp_version}" compile "com.squareup.okhttp3:logging-interceptor:$ {okhttp_version}" compile "com.squareup.retrofit2:retrofit:${retrofit_version}" compile "com.jakewharton.retrofit:retrofit2-rxjava2-adapter:$ {retrofit2_rxjava2_version}" compile “com.squareup.retrofit2:converter-gson:${retrofit_version}" : }
ϥΠϒϥϦͷಋೖ dependencies { : compile "com.google.code.gson:gson:${gson_version}" // RxJava2 compile "io.reactivex.rxjava2:rxjava:${rxjava2_version}"
compile "io.reactivex.rxjava2:rxandroid:${rxandroid2_version}" // Utils compile "com.google.code.gson:gson:${gson_version}" // Square compile “com.squareup.okhttp3:okhttp:${okhttp_version}" compile "com.squareup.okhttp3:logging-interceptor:$ {okhttp_version}" compile "com.squareup.retrofit2:retrofit:${retrofit_version}" compile "com.jakewharton.retrofit:retrofit2-rxjava2-adapter:$ {retrofit2_rxjava2_version}" compile “com.squareup.retrofit2:converter-gson:${retrofit_version}" : } RxJava2, RxAndroid: RxJava2Λ࣮ݱ͢ΔϥΠϒϥϦ
ϥΠϒϥϦͷಋೖ dependencies { : // RxJava2 compile "io.reactivex.rxjava2:rxjava:${rxjava2_version}" compile "io.reactivex.rxjava2:rxandroid:${rxandroid2_version}"
// Utils compile "com.google.code.gson:gson:${gson_version}" // Square compile “com.squareup.okhttp3:okhttp:${okhttp_version}" compile "com.squareup.okhttp3:logging-interceptor:$ {okhttp_version}" compile "com.squareup.retrofit2:retrofit:${retrofit_version}" compile "com.jakewharton.retrofit:retrofit2-rxjava2-adapter:$ {retrofit2_rxjava2_version}" compile “com.squareup.retrofit2:converter-gson:${retrofit_version}" : } Gson: JsonͱJavaΦϒδΣΫτΛ૬ޓม͢Δ
ϥΠϒϥϦͷಋೖ dependencies { : // RxJava2 compile "io.reactivex.rxjava2:rxjava:${rxjava2_version}" compile "io.reactivex.rxjava2:rxandroid:${rxandroid2_version}"
// Utils compile "com.google.code.gson:gson:${gson_version}" // Square compile “com.squareup.okhttp3:okhttp:${okhttp_version}" compile "com.squareup.okhttp3:logging-interceptor:$ {okhttp_version}" compile "com.squareup.retrofit2:retrofit:${retrofit_version}" compile "com.jakewharton.retrofit:retrofit2-rxjava2-adapter:$ {retrofit2_rxjava2_version}" compile “com.squareup.retrofit2:converter-gson:${retrofit_version}" : } OkHttp: ௨৴ϥΠϒϥϦ
ϥΠϒϥϦͷಋೖ dependencies { : // RxJava2 compile "io.reactivex.rxjava2:rxjava:${rxjava2_version}" compile "io.reactivex.rxjava2:rxandroid:${rxandroid2_version}"
// Utils compile "com.google.code.gson:gson:${gson_version}" // Square compile “com.squareup.okhttp3:okhttp:${okhttp_version}" compile "com.squareup.okhttp3:logging-interceptor:$ {okhttp_version}" compile "com.squareup.retrofit2:retrofit:${retrofit_version}" compile "com.jakewharton.retrofit:retrofit2-rxjava2-adapter:$ {retrofit2_rxjava2_version}" compile “com.squareup.retrofit2:converter-gson:${retrofit_version}" : } Retrofit: ௨৴ϥΠϒϥϦ
ϥΠϒϥϦͷಋೖ dependencies { : // RxJava2 compile "io.reactivex.rxjava2:rxjava:${rxjava2_version}" compile "io.reactivex.rxjava2:rxandroid:${rxandroid2_version}"
// Utils compile "com.google.code.gson:gson:${gson_version}" // Square compile “com.squareup.okhttp3:okhttp:${okhttp_version}" compile "com.squareup.okhttp3:logging-interceptor:$ {okhttp_version}" compile "com.squareup.retrofit2:retrofit:${retrofit_version}" compile "com.jakewharton.retrofit:retrofit2-rxjava2-adapter:$ {retrofit2_rxjava2_version}" compile “com.squareup.retrofit2:converter-gson:${retrofit_version}" : } Retrofit: Ξμϓλ
Ϟσϧͷఆٛ • ConnpassͷϨεϙϯε༷ʹ߹ΘͤͯϞσϧΛఆٛ • ଟ͋Γ·͕͢ɺ αϯϓϧͳͷͰ Ұ෦Ͱߏஙͯ͠·͢ public class ConnpassEvent
{ @SerializedName("results_returned") public int results; public List<Events> events; public static class Events { public String event_id; public String title; @SerializedName("catch") public String subTitle; public String description; } }
REST APIͷ༷Λఆٛ • Interface ͱͯ͠ϦΫΤετͷ༷͕·ͱΊΒΕΔͷ͕ Retrofit ͷັྗ public interface ConnpassApi
{ String API_END_POINT = "https://connpass.com/api/v1/"; @GET("event/") Observable<ConnpassEvent> getEvent( @Query("event_id") long eventId ); } // ্ه Interface ʹΑΓɺhttps://connpass.com/api/v1/ event/?event_id={eventId} ͱ͍͏ϦΫΤετ͕؆୯ʹ࣮Ͱ͖Δ
REST APIͷ༷Λఆٛ • Interface ͱͯ͠ϦΫΤετͷ༷͕·ͱΊΒΕΔͷ͕ Retrofit ͷັྗ public interface ConnpassApi
{ String API_END_POINT = "https://connpass.com/api/v1/"; @GET("event/") Observable<ConnpassEvent> getEvent( @Query("event_id") long eventId ); } // ্ه Interface ʹΑΓɺhttps://connpass.com/api/v1/ event/?event_id={eventId} ͱ͍͏ϦΫΤετ͕؆୯ʹ࣮Ͱ͖Δ REST APIͷΤϯυϙΠϯτఆٛ HTTP GETͷཁٻͰɺ”event/“APIͷཁٻͰ͋Δ͜ ͱ͕ಡΈऔΕΔ
REST APIͷ༷Λఆٛ • Interface ͱͯ͠ϦΫΤετͷ༷͕·ͱΊΒΕΔͷ͕ Retrofit ͷັྗ public interface ConnpassApi
{ String API_END_POINT = "https://connpass.com/api/v1/"; @GET("event/") Observable<ConnpassEvent> getEvent( @Query("event_id") long eventId ); } // ্ه Interface ʹΑΓɺhttps://connpass.com/api/v1/ event/?event_id={eventId} ͱ͍͏ϦΫΤετ͕؆୯ʹ࣮Ͱ͖Δ ˏQueryΞϊςʔγϣϯͰ”event_id”ͷύϥϝʔλ ʹҾͰ͞Ε͖ͯͨΛࢦఆ͢Δ ΓͷܕΛ Observable<ConnpassEvent> ʹ͢ Δ͜ͱͰɺఆٛͨ͠ϞσϧʹGsonܗࣜͰࣗಈͰ૬ ޓม͕ߦΘΕΔ
REST APIͷ༷Λఆٛ • Interface ͱͯ͠ϦΫΤετͷ༷͕·ͱΊΒΕΔͷ͕ Retrofit ͷັྗ public interface ConnpassApi
{ String API_END_POINT = "https://connpass.com/api/v1/"; @GET("event/") Observable<ConnpassEvent> getEvent( @Query("event_id") long eventId ); } // ্ه Interface ʹΑΓɺhttps://connpass.com/api/v1/ event/?event_id={eventId} ͱ͍͏ϦΫΤετ͕؆୯ʹ࣮Ͱ͖Δ
Retrofit ͷੜ public class RetrofitManager { public static Observable<ConnpassEvent> getEvent(long
eventId) { HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor() .setLevel(HttpLoggingInterceptor.Level.BASIC); OkHttpClient okHttpClient = new OkHttpClient.Builder() .addInterceptor(interceptor) .build(); Gson gson = new GsonBuilder() .serializeNulls() .create(); Retrofit retrofit = new Retrofit.Builder() .baseUrl(ConnpassApi.API_END_POINT) .client(okHttpClient) .addConverterFactory(GsonConverterFactory.create(gson)) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build(); return retrofit.create(ConnpassApi.class) .getEvent(eventId); } }
Retrofit ͷੜ public class RetrofitManager { public static Observable<ConnpassEvent> getEvent(long
eventId) { HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor() .setLevel(HttpLoggingInterceptor.Level.BASIC); OkHttpClient okHttpClient = new OkHttpClient.Builder() .addInterceptor(interceptor) .build(); Gson gson = new GsonBuilder() .serializeNulls() .create(); Retrofit retrofit = new Retrofit.Builder() .baseUrl(ConnpassApi.API_END_POINT) .client(okHttpClient) .addConverterFactory(GsonConverterFactory.create(gson)) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build(); return retrofit.create(ConnpassApi.class) .getEvent(eventId); } } HTTP௨৴ͷঢ়گΛϩΪϯάʢσόοάʣ͍ͨ͠ ߹ʹఆٛ
Retrofit ͷੜ public class RetrofitManager { public static Observable<ConnpassEvent> getEvent(long
eventId) { HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor() .setLevel(HttpLoggingInterceptor.Level.BASIC); OkHttpClient okHttpClient = new OkHttpClient.Builder() .addInterceptor(interceptor) .build(); Gson gson = new GsonBuilder() .serializeNulls() .create(); Retrofit retrofit = new Retrofit.Builder() .baseUrl(ConnpassApi.API_END_POINT) .client(okHttpClient) .addConverterFactory(GsonConverterFactory.create(gson)) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build(); return retrofit.create(ConnpassApi.class) .getEvent(eventId); } } ઌఔͷHttpLoggingInterceptorΛՃͨ͠ OkHttpClientͷఆٛ
Retrofit ͷੜ public class RetrofitManager { public static Observable<ConnpassEvent> getEvent(long
eventId) { HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor() .setLevel(HttpLoggingInterceptor.Level.BASIC); OkHttpClient okHttpClient = new OkHttpClient.Builder() .addInterceptor(interceptor) .build(); Gson gson = new GsonBuilder() .serializeNulls() .create(); Retrofit retrofit = new Retrofit.Builder() .baseUrl(ConnpassApi.API_END_POINT) .client(okHttpClient) .addConverterFactory(GsonConverterFactory.create(gson)) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build(); return retrofit.create(ConnpassApi.class) .getEvent(eventId); } } JSONͰnull͕͞Ε͖ͯͨͱ͖ʹɺGsonͰॲཧ͢ ΔͨΊʹ serializeNulls() Λࢦఆͨ͠ Gson ΦϒδΣ ΫτΛੜ͢Δ
Retrofit ͷੜ public class RetrofitManager { public static Observable<ConnpassEvent> getEvent(long
eventId) { HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor() .setLevel(HttpLoggingInterceptor.Level.BASIC); OkHttpClient okHttpClient = new OkHttpClient.Builder() .addInterceptor(interceptor) .build(); Gson gson = new GsonBuilder() .serializeNulls() .create(); Retrofit retrofit = new Retrofit.Builder() .baseUrl(ConnpassApi.API_END_POINT) .client(okHttpClient) .addConverterFactory(GsonConverterFactory.create(gson)) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build(); return retrofit.create(ConnpassApi.class) .getEvent(eventId); } } RetrofitͷΦϒδΣΫτΛੜ͢Δɻ addCallAdapterFactory()ͰRetrofitʹRxJava2Ͱॲ ཧͰ͖ΔΑ͏ʹΞμϓλʔΛઃఆ͍ͯ͠Δ
Retrofit ͷੜ public class RetrofitManager { public static Observable<ConnpassEvent> getEvent(long
eventId) { HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor() .setLevel(HttpLoggingInterceptor.Level.BASIC); OkHttpClient okHttpClient = new OkHttpClient.Builder() .addInterceptor(interceptor) .build(); Gson gson = new GsonBuilder() .serializeNulls() .create(); Retrofit retrofit = new Retrofit.Builder() .baseUrl(ConnpassApi.API_END_POINT) .client(okHttpClient) .addConverterFactory(GsonConverterFactory.create(gson)) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build(); return retrofit.create(ConnpassApi.class) .getEvent(eventId); } } ͜ΕΒͷ߲ΞϓϦέʔγϣϯͰҰఆٛ͢Δ ͱͦͷޙมΘΔඞཁͳ͍ͷͰɺDagger ͰInject ͢ΔͳͲͷ࣮͕·͍͠
σʔλͷऔಘͱө private ActivityMainBinding mBinding; private Disposable mDisposable; @Override protected void
onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main); // ຊAndroidͷձ தࠃࢧ෦ ୈ34ճษڧձͷΠϕϯτใΛऔಘ͢Δ mDisposable = RetrofitManager.getEvent(51822) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(event -> { if (event.results > 0) { mBinding.cjagEventId.setText(event.events.get(0).event_id); mBinding.cjagTitle.setText(event.events.get(0).title); mBinding.cjagCatch.setText(event.events.get(0).subTitle); mBinding.cjagDescription.loadData(event.events.get(0).description, "text/html; charset=UTF-8", "UTF-8"); } else { Toast.makeText(this, “σʔλ͕Έ͔ͭΓ·ͤΜͰͨ͠", Toast.LENGTH_SHORT).show(); } }, Throwable::printStackTrace); }
σʔλͷऔಘͱө private ActivityMainBinding mBinding; private Disposable mDisposable; @Override protected void
onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main); // ຊAndroidͷձ தࠃࢧ෦ ୈ34ճษڧձͷΠϕϯτใΛऔಘ͢Δ mDisposable = RetrofitManager.getEvent(51822) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(event -> { if (event.results > 0) { mBinding.cjagEventId.setText(event.events.get(0).event_id); mBinding.cjagTitle.setText(event.events.get(0).title); mBinding.cjagCatch.setText(event.events.get(0).subTitle); mBinding.cjagDescription.loadData(event.events.get(0).description, "text/html; charset=UTF-8", "UTF-8"); } else { Toast.makeText(this, “σʔλ͕Έ͔ͭΓ·ͤΜͰͨ͠", Toast.LENGTH_SHORT).show(); } }, Throwable::printStackTrace); } DataBinding ΛͬͯɺUIͷ߲ʹରͯ͠Λ ઃఆʢ͓·͚ʣ
σʔλͷऔಘͱө private ActivityMainBinding mBinding; private Disposable mDisposable; @Override protected void
onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main); // ຊAndroidͷձ தࠃࢧ෦ ୈ34ճษڧձͷΠϕϯτใΛऔಘ͢Δ mDisposable = RetrofitManager.getEvent(51822) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(event -> { if (event.results > 0) { mBinding.cjagEventId.setText(event.events.get(0).event_id); mBinding.cjagTitle.setText(event.events.get(0).title); mBinding.cjagCatch.setText(event.events.get(0).subTitle); mBinding.cjagDescription.loadData(event.events.get(0).description, "text/html; charset=UTF-8", "UTF-8"); } else { Toast.makeText(this, “σʔλ͕Έ͔ͭΓ·ͤΜͰͨ͠", Toast.LENGTH_SHORT).show(); } }, Throwable::printStackTrace); } RetrofitͰຊAndroidͷձͷΠϕϯτใΛཁٻ
σʔλͷऔಘͱө private ActivityMainBinding mBinding; private Disposable mDisposable; @Override protected void
onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main); // ຊAndroidͷձ தࠃࢧ෦ ୈ34ճษڧձͷΠϕϯτใΛऔಘ͢Δ mDisposable = RetrofitManager.getEvent(51822) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(event -> { if (event.results > 0) { mBinding.cjagEventId.setText(event.events.get(0).event_id); mBinding.cjagTitle.setText(event.events.get(0).title); mBinding.cjagCatch.setText(event.events.get(0).subTitle); mBinding.cjagDescription.loadData(event.events.get(0).description, "text/html; charset=UTF-8", "UTF-8"); } else { Toast.makeText(this, “σʔλ͕Έ͔ͭΓ·ͤΜͰͨ͠", Toast.LENGTH_SHORT).show(); } }, Throwable::printStackTrace); } subscribeOn()Ͱɺ͜͜·ͰͷϝιουνΣʔϯͷ ॲཧΛϫʔΧʔεϨου(Schedulers.io()) Ͱߦ͏͜ ͱΛࢦఆ .observeOn(AndroidSchedulers.mainThread())ɺ Ҏ߱ͷίʔϧόοΫॲཧΛUIεϨουͰߦ͏͜ͱΛ ࢦఆ
σʔλͷऔಘͱө private ActivityMainBinding mBinding; private Disposable mDisposable; @Override protected void
onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main); // ຊAndroidͷձ தࠃࢧ෦ ୈ34ճษڧձͷΠϕϯτใΛऔಘ͢Δ mDisposable = RetrofitManager.getEvent(51822) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(event -> { if (event.results > 0) { mBinding.cjagEventId.setText(event.events.get(0).event_id); mBinding.cjagTitle.setText(event.events.get(0).title); mBinding.cjagCatch.setText(event.events.get(0).subTitle); mBinding.cjagDescription.loadData(event.events.get(0).description, "text/html; charset=UTF-8", "UTF-8"); } else { Toast.makeText(this, “σʔλ͕Έ͔ͭΓ·ͤΜͰͨ͠", Toast.LENGTH_SHORT).show(); } }, Throwable:printStackTrace); } onSuccess()͕ίʔϧόοΫ࣌ͷॲཧ
σʔλͷऔಘͱө private ActivityMainBinding mBinding; private Disposable mDisposable; @Override protected void
onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main); // ຊAndroidͷձ தࠃࢧ෦ ୈ34ճษڧձͷΠϕϯτใΛऔಘ͢Δ mDisposable = RetrofitManager.getEvent(51822) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(event -> { if (event.results > 0) { mBinding.cjagEventId.setText(event.events.get(0).event_id); mBinding.cjagTitle.setText(event.events.get(0).title); mBinding.cjagCatch.setText(event.events.get(0).subTitle); mBinding.cjagDescription.loadData(event.events.get(0).description, "text/html; charset=UTF-8", "UTF-8"); } else { Toast.makeText(this, “σʔλ͕Έ͔ͭΓ·ͤΜͰͨ͠", Toast.LENGTH_SHORT).show(); } }, Throwable::printStackTrace); } onError() ͕ίʔϧόοΫ࣌ͷॲཧ
ղ์ॲཧ private Disposable mDisposable; @Override protected void onDestroy() { if
(mDisposable != null) { mDisposable.dispose(); } super.onDestroy(); }
·ͱΊ • RxJava2 ඇಉظʹڧ͘ɺϦετૢ࡞͕ڠྗ • Retrofit Λ͏ͱɺιʔείʔυ͕༷ʹͳΔ • Retrolambdaศརʘ(^o^)ʗ •
OkHttpΛclientͱͯ͠ࢦఆ͢ΕɺσόοάͳͲͰ͖ ָͯͪΜ
͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ m(_ _)m