一、什麼是 RxJava?
RxJava 是一個響應式程式設計框架,採用觀察者設計模式。所以自然少不了 Observable 和 Subscriber 這兩個東東了。
RxJava 是一個開源專案,地址:https://github.com/ReactiveX/RxJava
還有一個RxAndroid,用於 Android 開發,新增了 Android 用的介面。地址:https://github.com/ReactiveX/RxAndroid
二、例子
通過請求openweathermap 的天氣查詢介面返回天氣資料
1、增加編譯依賴
1 dependencies { 2 compile fileTree(dir: 'libs', include: ['*.jar']) 3 compile 'com.android.support:appcompat-v7:22.0.0' 4 compile 'io.reactivex:rxjava:1.0.9' 5 compile 'io.reactivex:rxandroid:0.24.0' 6 compile 'com.squareup.retrofit:retrofit:1.9.0' 7 }
retrofit 是一個 restful 請求客戶端。詳見:http://square.github.io/retrofit/
2、伺服器介面
1 /** 2 * 介面 3 * Created by Hal on 15/4/26. 4 */ 5 public class ApiManager { 6 7 private static final String ENDPOINT = "http://api.openweathermap.org/data/2.5"; 8 9 /** 10 * 服務介面 11 */ 12 private interface ApiManagerService { 13 @GET("/weather") 14 WeatherData getWeather(@Query("q") String place, @Query("units") String units); 15 } 16 17 private static final RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(ENDPOINT).setLogLevel(RestAdapter.LogLevel.FULL).build(); 18 19 private static final ApiManagerService apiManager = restAdapter.create(ApiManagerService.class); 20 21 /** 22 * 將服務介面返回的資料,封裝成{@link rx.Observable} 23 * @param city 24 * @return 25 */ 26 public static Observable<WeatherData> getWeatherData(final String city) { 27 return Observable.create(new Observable.OnSubscribe<WeatherData>() { 28 @Override 29 public void call(Subscriber<? super WeatherData> subscriber) { 30 //訂閱者回撥 onNext 和 onCompleted 31 subscriber.onNext(apiManager.getWeather(city, "metric")); 32 subscriber.onCompleted(); 33 } 34 }).subscribeOn(Schedulers.io()); 35 } 36 }
訂閱者的回撥有三個方法,onNext,onError,onCompleted
3、介面呼叫
1 /** 2 * 多個 city 請求 3 * map,flatMap 對 Observable進行變換 4 */ 5 Observable.from(CITIES).flatMap(new Func1<String, Observable<WeatherData>>() { 6 @Override 7 public Observable<WeatherData> call(String s) { 8 return ApiManager.getWeatherData(s); 9 } 10 }).subscribeOn(Schedulers.io()) 11 .observeOn(AndroidSchedulers.mainThread()) 12 .subscribe(/*onNext*/new Action1<WeatherData>() { 13 @Override 14 public void call(WeatherData weatherData) { 15 Log.d(LOG_TAG, weatherData.toString()); 16 } 17 }, /*onError*/new Action1<Throwable>() { 18 @Override 19 public void call(Throwable throwable) { 20 21 } 22 }); 23 24 /** 25 * 單個 city 請求 26 */ 27 ApiManager.getWeatherData(CITIES[0]).subscribeOn(Schedulers.io()) 28 .observeOn(AndroidSchedulers.mainThread()) 29 .subscribe(new Action1<WeatherData>() { 30 @Override 31 public void call(WeatherData weatherData) { 32 Log.d(LOG_TAG, weatherData.toString()); 33 ((TextView) findViewById(R.id.text)).setText(weatherData.toString()); 34 } 35 }, new Action1<Throwable>() { 36 @Override 37 public void call(Throwable throwable) { 38 Log.e(LOG_TAG, throwable.getMessage(), throwable); 39 } 40 }); 41 42 /** 43 * Android View 事件處理 44 */ 45 ViewObservable.clicks(findViewById(R.id.text), false).subscribe(new Action1<OnClickEvent>() { 46 @Override 47 public void call(OnClickEvent onClickEvent) { 48 49 } 50 });
subscribeOn(Schedulers.io())與observeOn(AndroidSchedulers.mainThread())分別定義了這兩個動作的執行緒。Android UI 更新需要在主執行緒。
4、retrofit 支援 rxjava 整合
1 /** 2 * 服務介面 3 */ 4 private interface ApiManagerService { 5 @GET("/weather") 6 WeatherData getWeather(@Query("q") String place, @Query("units") String units); 7 8 /** 9 * retrofit 支援 rxjava 整合 10 * 這種方法適用於新介面 11 */ 12 @GET("/weather") 13 Observable<WeatherData> getWeatherData(@Query("q") String place, @Query("units") String units); 14 }
--------EOF-----