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
2017/03/25 RxJava2 + OkHttp + Retrofit入門
Search
shige0501
March 25, 2017
Programming
1.8k
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
350
モバイルアプリを効率的に開発するクロスプラットフォーム開発という選択肢
shige0501
1
430
Other Decks in Programming
See All in Programming
Built Our Own Background Agent at LayerX
layerx
PRO
10
5.4k
いまどきの Codex で開発する visionOS アプリの開発スタイルについて
karad
0
140
AWS DevOps AgentのAzure接続機能を検証して見えた活用法/Use Cases Verified for the AWS DevOps Agent's Azure Connectivity Feature
masakiokuda
1
230
Google Apps Script で Ruby を動かす
kawahara
0
220
楽しそうなつよつよエンジニアと目が死んでる僕/A brilliant engineer having a blast, and dead-eyed me.
3l4l5
2
180
Flow は今どうなっているか
mizdra
PRO
0
410
自動化したのに回らない テスト運用の壁―AI時代の品質責任と生産性
mfunaki
0
170
Building a Meta Ray-Ban display app
akkeylab
0
140
Foundation Models frameworkで画像分析
ryodeveloper
1
610
S3 を使うアプリケーションをローカル完結で動かすことに全力を注いでみた / Running S3 Apps Offline
contour_gara
0
470
Jindong: Introducing Declarative Haptics in Compose Multiplatform
l2hyunwoo
0
110
進化を続けるGo toolsの現在地 / The Current State of Ever-Evolving Go Tools
hond0413
0
210
Featured
See All Featured
Accessibility Awareness
sabderemane
1
180
Leading Effective Engineering Teams in the AI Era
addyosmani
9
2.3k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
6k
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.5k
Producing Creativity
orderedlist
PRO
348
40k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
201
75k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
12
1.2k
Ruling the World: When Life Gets Gamed
codingconduct
0
300
Building a A Zero-Code AI SEO Workflow
portentint
PRO
0
660
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
490
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.5k
Agile that works and the tools we love
rasmusluckow
331
22k
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