Upgrade to Pro — share decks privately, control downloads, hide ads and more …

2017/03/25 RxJava2 + OkHttp + Retrofit入門

8afe178ab52fc72af60457600ab77362?s=47 shige0501
March 25, 2017

2017/03/25 RxJava2 + OkHttp + Retrofit入門

日本Androidの会 中国支部 第34回勉強会の資料です
https://cjag.connpass.com/event/51822/

8afe178ab52fc72af60457600ab77362?s=128

shige0501

March 25, 2017
Tweet

Transcript

  1. RxJava2 + OkHttp + Retrofit ೖ໳ 2017/03/25 ೔ຊAndroidͷձ தࠃࢧ෦ ॏଜ

    ߒೋ
  2. ࣗݾ঺հ • ໊લ • ॏଜ ߒೋ (Koji Shigemura) • ॴଐ

    • ೔ຊAndroidͷձ
 ίϛϡχςΟӡӦҕһ
 தࠃࢧ෦௕ • ChatWorkגࣜձࣾ
 ϞόΠϧɾιϑτ΢ΣΞΤϯδχΞ (Android)
 http://chatwork.com/
  3. ޷ධൃചதʂ Android SDKϙέοτϦϑΝϨϯε https://www.amazon.co.jp/dp/477416335X

  4. ΞδΣϯμ • RxJava2 ֓ཁ • OkHttp ֓ཁ • Retrofit ֓ཁ

    • ૊Έ߹Θ࣮ͤͨ૷
  5. RxJava2 ֓ཁ

  6. RxJava ͱ͸ʁ • Microsoft ͷ .NET Framework ͷ࣮ݧతϥΠϒϥϦͱͯ͠ 2009೥ʹੜ·ΕͨʮReactive Extensionsʯ͕2012೥ʹ

    ΦʔϓϯιʔεԽ
 ReactiveX: http://reactivex.io/ • ޙʹ Netflix ͕ Java ʹҠ২͠ɺΦʔϓϯιʔεԽͨ͠ͷ͕ RxJava
 RxJava: https://github.com/ReactiveX/RxJava • ϦΞΫςΟϒϓϩάϥϛϯά͕࣮ફ͠΍͍͢
  7. RxJava2 • Reactive Streams ʹ४ڌ
 Reactive Streams: http://www.reactive-streams.org/ • RxJava

    1.x͸ Reactive Streams ͷ࢓༷͕ग़ͯདྷΔલ͔ ΒϦϦʔε͞Ε͍ͯΔͷͰɺରԠͰ͖͍ͯͳ͍
 
 ࠓ೔͸ RxJava 1.xܥʹ͸৮Ε·ͤΜ
  8. ྫ (਺ࣈͷϦετΛॲཧ͢Δ) 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
  9. ྫ (਺ࣈͷϦετΛॲཧ͢Δ) 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ͷ഑ྻΛఆٛ
  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 ۮ਺ͷϦετΛ࡞Γɺฦ͢
  11. ྫ (਺ࣈͷϦετΛॲཧ͢Δ) 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() Ͱจࣈྻʹͯ͠ग़ྗ
  12. ྫ (਺ࣈͷϦετΛॲཧ͢Δ) 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
  13. ྫ (਺ࣈͷϦετΛॲཧ͢Δ) 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ͱॱ൪ʹ࣍ͷϝιουʹྲྀΕΔʣ
  14. ྫ (਺ࣈͷϦετΛॲཧ͢Δ) 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) ͷ΋ͷΛϦετͱͯ͠ɺ໭Γ஋ͱͯ͠ฦ͢
  15. ྫ (਺ࣈͷϦετΛॲཧ͢Δ) 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()Ͱத਎Λग़ྗ
  16. OkHttp ֓ཁ

  17. OkHttp ͱ͸ʁ • Square ͕ࣾఏڙ͢Δ HTTP௨৴ ͱ SPDY௨৴͢ΔͨΊ ͷΦʔϓϯιʔεϥΠϒϥϦ
 OkHttp:

    http://square.github.io/okhttp/
  18. ͳͥ OkHttp ͳͷ͔ʁ • Android SDK ʹ૊Έࠐ·Ε͍ͯͨ Apache HTTP Client

    ͕ Android 6.0 (Marshmallow) Ͱ Deprecatedʹ • ҆ఆͯ͠࢖͑Δ௨৴ΫϥΠΞϯτͱͯ͠ɺOkHttp͕ݱঢ় ͷσϑΝΫτελϯμʔυ
  19. OkHttpͷྫ͸লུ
 ʢࠓճͷϝΠϯ͸RetrofitͳͷͰʣ

  20. Retrofit֓ཁ

  21. Retrofitͱ͸ʁ • Square ͕ࣾ։ൃͨ͠ܕ҆શͳHTTP Client
 Retrofit: http://square.github.io/retrofit/ • REST௨৴͕؆ܿԽͰ͖ɺPOJOม׵΋ͯ͘͠ΕΔ •

    ๛෋ͳίϯόʔλ • GSON, Jackson, Moshi, Protobuf….. and more.
  22. ૊Έ߹Θ࣮ͤͨ૷

  23. ࠓճͷαϯϓϧ࢓༷ • Connpass͔Βࠓճͷษڧձͷ৘ใΛऔಘ͠ɺը໘ʹද ࣔ͢Δ • ConnpassͷAPI࢓༷:
 https://connpass.com/about/api/ • αϯϓϧϓϩδΣΫτ: 


    https://github.com/shige0501/retrofit_sample

  24. ࣮ߦ݁Ռ

  25. લఏ • Retrolambda Λಋೖ͠ɺϥϜμࣜͰ࣮૷͍ͯ͠·͢ • αϯϓϧͷՄಡੑΛҙࣝͯ͠ɺ͋͑ͯจࣈྻΛϦιʔε Խ͍ͯ͠·ͤΜ

  26. ϥΠϒϥϦͷಋೖ 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}" : }
  27. ϥΠϒϥϦͷಋೖ 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Λ࣮ݱ͢ΔϥΠϒϥϦ
  28. ϥΠϒϥϦͷಋೖ 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ΦϒδΣΫτΛ૬ޓม׵͢Δ
  29. ϥΠϒϥϦͷಋೖ 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: ௨৴ϥΠϒϥϦ
  30. ϥΠϒϥϦͷಋೖ 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: ௨৴ϥΠϒϥϦ
  31. ϥΠϒϥϦͷಋೖ 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: Ξμϓλ
  32. Ϟσϧͷఆٛ • 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; } }
  33. 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} ͱ͍͏ϦΫΤετ͕؆୯ʹ࣮૷Ͱ͖Δ
  34. 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ͷཁٻͰ͋Δ͜ ͱ͕ಡΈऔΕΔ
  35. 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ܗࣜͰࣗಈͰ૬ ޓม׵͕ߦΘΕΔ
  36. 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} ͱ͍͏ϦΫΤετ͕؆୯ʹ࣮૷Ͱ͖Δ
  37. 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); } }
  38. 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௨৴ͷঢ়گΛϩΪϯάʢσόοάʣ͍ͨ͠৔ ߹ʹఆٛ
  39. 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ͷఆٛ
  40. 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 ΦϒδΣ ΫτΛੜ੒͢Δ
  41. 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Ͱॲ ཧͰ͖ΔΑ͏ʹΞμϓλʔΛઃఆ͍ͯ͠Δ
  42. 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 ͢ΔͳͲͷ࣮૷͕๬·͍͠
  43. σʔλͷऔಘͱ൓ө 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); }
  44. σʔλͷऔಘͱ൓ө 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ͷ߲໨ʹରͯ͠௚઀஋Λ ઃఆʢ͓·͚ʣ
  45. σʔλͷऔಘͱ൓ө 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ͷձͷΠϕϯτ৘ใΛཁٻ
  46. σʔλͷऔಘͱ൓ө 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εϨουͰߦ͏͜ͱΛ ࢦఆ
  47. σʔλͷऔಘͱ൓ө 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()͕ίʔϧόοΫ࣌ͷॲཧ
  48. σʔλͷऔಘͱ൓ө 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() ͕ίʔϧόοΫ࣌ͷॲཧ
  49. ղ์ॲཧ private Disposable mDisposable; @Override protected void onDestroy() { if

    (mDisposable != null) { mDisposable.dispose(); } super.onDestroy(); }
  50. ·ͱΊ • RxJava2 ͸ඇಉظʹڧ͘ɺϦετૢ࡞͕ڠྗ • Retrofit Λ࢖͏ͱɺιʔείʔυ͕࢓༷ʹͳΔ • Retrolambdaศརʘ(^o^)ʗ •

    OkHttpΛclientͱͯ͠ࢦఆ͢Ε͹ɺσόοάͳͲ΋Ͱ͖ ָͯͪΜ
  51. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ m(_ _)m