RxJava操做符的簡單使用

1、準備工做在app的build.gradle文件下的dependencies中添加依賴:java

    compile 'io.reactivex:rxjava:1.3.0'
    compile 'io.reactivex:rxandroid:1.2.1'

2、RxJava的操做符用法:react

一、createandroid

//create  建立Observable
        //Observable 被觀察者
        //Subscribers觀察者

        Observable<String> observable = Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                Log.i(TAG,"call.....");
                if(!subscriber.isUnsubscribed()) {
                    subscriber.onNext("我被執行了1"); //只有完成了下面的訂閱方法才能執行onNext等方法
                    subscriber.onNext("我被執行了2");
                    subscriber.onNext("我被執行了3");
                    subscriber.onNext("我被執行了4");
                    subscriber.onNext("我被執行了5");
                    subscriber.onNext("我被執行了6");

                    subscriber.onCompleted();
                }
            }
        });

        //完成訂閱
        observable.subscribe(new Subscriber<String>() {
            @Override
            public void onCompleted() {
                Log.i(TAG,"onCompleted") ;
            }

            @Override
            public void onError(Throwable e) {
                Log.i(TAG,"onError:"+e.getMessage());
            }

            @Override
            public void onNext(String s) {
                Log.i(TAG,"onNext:"+s);
            }
        }) ;

二、from數組

//from 也能夠建立Observable  講int類型的數組01234轉化爲Observable,經過訂閱的onNext能夠一個個的取到數組的值
        Integer[] arr = {0,1,2,3,4};
        //完成建立Observable
        Observable<Integer> from = Observable.from(arr);
        //完成訂閱
        from.subscribe(new Subscriber<Integer>() {
            @Override
            public void onCompleted() {
                Log.i(TAG,"onCompleted") ;
            }

            @Override
            public void onError(Throwable e) {
                Log.i(TAG,"onError:"+e.getMessage());
            }

            @Override
            public void onNext(Integer i) {
                Log.i(TAG,"onNext:"+i);
            }
        }) ;

三、just網絡

//just  也能夠建立Observable  參數是可變參數
        Observable.just(0, 1, 2, 3, 4, 5)
                .subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer integer) {
                        Log.i(TAG,"call:"+integer);
                    }
                }) ;

四、mapapp

//map 變換,在該實例中,將Integer轉換成String
//在實際開發中,須要經過String類型的url獲取到圖片

        String[] urls = {};
        Observable.from(urls)
                .map(new Func1<String, Bitmap>() { //第一個參數爲傳入的類型,第二個參數爲返回的類型
                    @Override
                    public Bitmap call(String s) {
                        //s 表明圖片url,網絡請求經過url 獲取到圖片
                        return null;
                    }
                }).subscribe(new Action1<Bitmap>() { // 獲取的的Bitmap類型再進行訂閱處理
            @Override
            public void call(Bitmap bitmap) {
                //iv.setBackage()
            }
        });

        /*Observable.just(0,1,2,3)
                .map(new Func1<Integer, String>() {
                    @Override
                    public String call(Integer integer) {
                        return integer+"轉換了";
                    }
                }).subscribe(new Action1<String>() {
            @Override
            public void call(String s) {
                Log.i(TAG,"call:"+s);//0轉換了  1轉換了。。。
            }
        });*/

五、flatMap框架

//flatmap 轉換  將多個Observable轉換成一個Observalbe而後發送
        //獲取城市的天氣

        /**
         *
         * map 是將一種類型轉換成另外一種類型(能夠是任意類型)
         *
         * flatmap 是將一種類轉換成Observable(泛型能夠是任意的)
         *
         */

        String[] citys = {"北京","上海","杭州"};
        Observable.from(citys)
                .flatMap(new Func1<String, Observable<WeathData>>() {
                    @Override
                    public Observable<WeathData> call(String s) {
                        return getCityWeathData(s);
                    }
                }).subscribe(new Action1<WeathData>() {
            @Override
            public void call(WeathData weathData) {
                Log.i(TAG,weathData.city+weathData.state);
            }
        });

此處用到的getCityWeathData方法返回出來一個泛型爲WeathData的Observable:ide

/**
     * 獲取一個城市的天氣數據
     * @param city
     * @return
     */
    private Observable<WeathData> getCityWeathData(final String city){
        return Observable.just(city)
                .map(new Func1<String, WeathData>() {
                    @Override
                    public WeathData call(String s) {
                        //經過網絡請求獲取城市的天氣數據
                        WeathData weathData = new WeathData();
                        weathData.city = city ;
                        weathData.state = "晴天" ;
                        return weathData ;
                    }
                });
    }

六、zipgradle

//zip  將兩個Observable按照規則嚴格的合成一個Observable
        Observable<Integer> observable1 = Observable.just(10, 20, 30,40);
        Observable<Integer> observable2 = Observable.just(1, 2, 3,4);

        Observable.zip(observable1, observable2, new Func2<Integer, Integer, String>() {
            @Override
            public String call(Integer integer, Integer integer2) {
                //定義合併規則
                return integer + integer2 + "abc";
            }
        }).subscribe(new Action1<String>() {
            @Override
            public void call(String string) {
                Log.i(TAG,"call:"+string) ;
            }
        });

七、zipWithui

//zipwith 將自己與其餘的Observable按照規則嚴格的合併成一個Observable
        Observable.just(10,20,30,40)
                .zipWith(Observable.just("a", "b", "c"), new Func2<Integer, String, String>() {
                    @Override
                    public String call(Integer integer, String s) {
                        //合併規則
                        return integer + s ;
                    }
                }).subscribe(new Action1<String>() {
            @Override
            public void call(String s) {
                Log.i(TAG,"call:"+s) ;
            }
        });

八、retry

//retry 在出錯的時候重試(異常的時候從新執行)
        //用處:網絡鏈接異常的時候
        Observable.create(new Observable.OnSubscribe<Integer>() {
            @Override
            public void call(Subscriber<? super Integer> subscriber) {
                try {
                    for (int i = 0; i < 5; i++) {
                        if (i == 3) {
                            throw new Exception("出錯了");
                        }
                        subscriber.onNext(i);
                    }
                    subscriber.onCompleted();
                }catch (Exception e){
                    subscriber.onError(e);
                }
            }
        }).retry(2).subscribe(new Subscriber<Integer>() {
            @Override
            public void onCompleted() {
                Log.i(TAG,"onCompleted");
            }

            @Override
            public void onError(Throwable e) {
                Log.i(TAG,"onError:"+e.getMessage()) ;
            }

            @Override
            public void onNext(Integer integer) {
                Log.i(TAG,"onNext:"+integer) ;
            }
        });

九、retryWhen

//retrywhen 異常的時候執行
        //網絡請求框架中,通常使用retryWhen  要執行操做是鏈接網絡,鏈接出異常的時候,
        // 一、咱們能夠直接重複執行鏈接網絡,retry
        // 二、同時咱們也能夠判斷鏈接異常的類型,再作決定是否重連 retyrWhen
        Observable.create(new Observable.OnSubscribe<Integer>() {
            @Override
            public void call(Subscriber<? super Integer> subscriber) {
                Log.i(TAG,"總出錯");
                subscriber.onError(new Throwable("出錯了"));
            }
        }).retryWhen(new Func1<Observable<? extends Throwable>, Observable<?>>() {
            @Override
            public Observable<?> call(Observable<? extends Throwable> observable) {
                return observable.zipWith(Observable.range(1, 3), new Func2<Throwable, Integer, Integer>() {
                    @Override
                    public Integer call(Throwable throwable, Integer integer) {
                        return integer;
                    }
                }).flatMap(new Func1<Integer, Observable<?>>() {
                    @Override
                    public Observable<?> call(Integer integer) {
                        //timer 延遲執行的操做符
                        Log.i(TAG,"延遲"+integer+"秒");
                        return Observable.timer(integer, TimeUnit.SECONDS);
                    }
                });
            }
        }).subscribe(new Subscriber<Integer>() {
            @Override
            public void onCompleted() {
                Log.i(TAG,"onCompleted");
            }

            @Override
            public void onError(Throwable e) {
                Log.i(TAG,"onError:"+e.getMessage()) ;
            }

            @Override
            public void onNext(Integer integer) {
                Log.i(TAG,"onNext:"+integer) ;
            }
        }) ;

十、filter

//filter 按照規則過濾
        Observable.just(0,1,2,3,4,5)
                .filter(new Func1<Integer, Boolean>() {
                    @Override
                    public Boolean call(Integer integer) {
                        return integer < 2;
                    }
                }).subscribe(new Action1<Integer>() {
            @Override
            public void call(Integer integer) {
                Log.i(TAG,"call:"+integer) ;
            }
        });

。。。

相關文章
相關標籤/搜索