RxJava和RxAndroid

如今RxJava和RxAndroid愈來愈火爆,本身在業餘時間也學習了一下,感受確實很好用,以前 爲了完成頁面刷新,數據請求,組件信息傳遞的時候,要使用handler,真的是邏輯思路很強,稍微不注意,就各類錯誤一大堆。這下有了RxJava和RxAndroid,真的爽。
RxJava

網上有不少給RxJava作定義的,不少人說的比較官方,而我是比較笨的那種人,因此看了好久也沒有看懂,不過最後在一個博客中,看到

    RxJava就是異步

我決定這句話歸納的仍是很是好的!!!

 


不過咱們仍是要來先看一下官方給出的定義
定義

    RxJava:a library for composing asynchronous and event-based programs using observable sequences for the Java VM
    // 翻譯:RxJava 是一個在 Java VM 上使用可觀測的序列來組成異步的、基於事件的程序的庫

因此說,RxJava 是一個 基於事件流、實現異步操做的庫
特色:

RxJava的使用方式是:基於事件流的鏈式調用
1. 邏輯簡潔
2. 實現優雅
3. 使用簡單
更重要的是,隨着程序邏輯的複雜性提升,它依然可以保持簡潔 & 優雅
原理

這個地方使用的是別人的例子,我以爲很好
顧客到餐廳吃飯

 

 

RxJava是一種基於擴展的觀察者模式
在RxJava中有四種角色:
角色     做用     類比
被觀察者(Observable)     產生事件     顧客
觀察者(Observer)     接收事件,並給出響應動做     廚房
訂閱(Subscribe)     鏈接 被觀察者 & 觀察者     服務員
事件(Event)     被觀察者 & 觀察者 溝通的載體     菜式

 

 


即RxJava原理可總結爲:被觀察者 (Observable) 經過 訂閱(Subscribe) 按順序發送事件(Event) 給觀察者 (Observer), 觀察者(Observer) 按順序接收事件 & 做出對應的響應動做。具體以下圖

 


配置

compile 'io.reactivex.rxjava2:rxjava:2.0.1'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'

RxJava的使用

使用方式:
1. 分步驟實現:該方法主要爲了深刻說明Rxjava的原理 & 使用,主要用於演示說明
2. 基於事件流的鏈式調用:主要用於實際使用
分步實現的步驟


    建立被觀察者(Obserable)和生產事件
        即:顧客-坐下餐桌-點菜
        具體實現:

// 1. 建立被觀察者 Observable 對象
        Observable<Integer> observable = Observable.create(new ObservableOnSubscribe<Integer>() {
          // create() 是 RxJava 最基本的創造事件序列的方法
          // 此處傳入了一個 OnSubscribe 對象參數
          // 當 Observable 被訂閱時,OnSubscribe 的 call() 方法會自動被調用,即事件序列就會依照設定依次被觸發
          // 即觀察者會依次調用對應事件的複寫方法從而響應事件
          // 從而實現被觀察者調用了觀察者的回調方法 & 由被觀察者向觀察者的事件傳遞,即觀察者模式

        // 2. 在複寫的subscribe()裏定義須要發送的事件
            @Override
            public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
                // 經過 ObservableEmitter類對象產生事件並通知觀察者
                // ObservableEmitter類介紹
                    // a. 定義:事件發射器
                    // b. 做用:定義須要發送的事件 & 向觀察者發送事件
                emitter.onNext(1);
                emitter.onNext(2);
                emitter.onNext(3);
                emitter.onComplete();
            }
        });


        <--擴展:RxJava 提供了其餘方法用於 建立被觀察者對象Observable -->
// 方法1:just(T...):直接將傳入的參數依次發送出來
  Observable observable = Observable.just("A", "B", "C");
  // 將會依次調用:
  // onNext("A");
  // onNext("B");
  // onNext("C");
  // onCompleted();

// 方法2:from(T[]) / from(Iterable<? extends T>) : 將傳入的數組 / Iterable 拆分紅具體對象後,依次發送出來
  String[] words = {"A", "B", "C"};
  Observable observable = Observable.from(words);
  // 將會依次調用:
  // onNext("A");
  // onNext("B");
  // onNext("C");
  // onCompleted();


    關於onComplete和onError惟一而且互斥這一點, 是須要自行在代碼中進行控制, 若是你的代碼邏輯中違背了這個規則, 並不必定會致使程序崩潰. 好比發送多個onComplete是能夠正常運行的, 依然是收到第一個onComplete就再也不接收了, 但如果發送多個onError, 則收到第二個onError事件會致使程序會崩潰.

    建立觀察者 (Observer )並 定義響應事件的行爲
        即 開廚房 - 肯定對應菜式
        發生的事件類型包括:Next事件、Complete事件 & Error事件
        

 


        具體實現:

//方式1:採用Observer 接口 -- >
        // 1. 建立觀察者 (Observer )對象
        Observer<Integer> observer = new Observer<Integer>() {
            // 2. 建立對象時經過對應複寫對應事件方法 從而 響應對應事件

            // 觀察者接收事件前,默認最早調用複寫 onSubscribe()
            @Override
            public void onSubscribe(Disposable d) {
                Log.d(TAG, "開始採用subscribe鏈接");
            }

            // 當被觀察者生產Next事件 & 觀察者接收到時,會調用該複寫方法 進行響應
            @Override
            public void onNext(Integer value) {
                Log.d(TAG, "對Next事件做出響應" + value);
            }

            // 當被觀察者生產Error事件& 觀察者接收到時,會調用該複寫方法 進行響應
            @Override
            public void onError(Throwable e) {
                Log.d(TAG, "對Error事件做出響應");
            }

            // 當被觀察者生產Complete事件& 觀察者接收到時,會調用該複寫方法 進行響應
            @Override
            public void onComplete() {
                Log.d(TAG, "對Complete事件做出響應");
            }
        };

        //方式2:採用Subscriber 抽象類 -- >
        // 說明:Subscriber類 = RxJava 內置的一個實現了 Observer 的抽象類,對 Observer 接口進行了擴展

        // 1. 建立觀察者 (Observer )對象
        Subscriber<String> subscriber = new Subscriber<Integer>() {

            // 2. 建立對象時經過對應複寫對應事件方法 從而 響應對應事件
            // 觀察者接收事件前,默認最早調用複寫 onSubscribe()
            @Override
            public void onSubscribe(Subscription s) {
                Log.d(TAG, "開始採用subscribe鏈接");
            }

            // 當被觀察者生產Next事件 & 觀察者接收到時,會調用該複寫方法 進行響應
            @Override
            public void onNext(Integer value) {
                Log.d(TAG, "對Next事件做出響應" + value);
            }

            // 當被觀察者生產Error事件& 觀察者接收到時,會調用該複寫方法 進行響應
            @Override
            public void onError(Throwable e) {
                Log.d(TAG, "對Error事件做出響應");
            }

            // 當被觀察者生產Complete事件& 觀察者接收到時,會調用該複寫方法 進行響應
            @Override
            public void onComplete() {
                Log.d(TAG, "對Complete事件做出響應");
            }
        };

        //特別注意:2 種方法的區別,即Subscriber 抽象類與Observer 接口的區別-- >
        // 相同點:兩者基本使用方式徹底一致(實質上,在RxJava的 subscribe 過程當中,Observer老是會先被轉換成Subscriber再使用)
        // 不一樣點:Subscriber抽象類對 Observer 接口進行了擴展,新增了兩個方法:
        // 1. onStart():在還未響應事件前調用,用於作一些初始化工做
        // 2. unsubscribe():用於取消訂閱。在該方法被調用後,觀察者將再也不接收 & 響應事件
        // 調用該方法前,先使用 isUnsubscribed() 判斷狀態,肯定被觀察者Observable是否還持有觀察者Subscriber的引用,若是引用不能及時釋放,就會出現內存泄露


    經過訂閱(Subscribe)鏈接觀察者和被觀察者
        顧客找到服務員 - 點菜 - 服務員下單到廚房 - 廚房烹調
        具體實現:

observable.subscribe(observer);
 // 或者 observable.subscribe(subscriber);
 ```
 擴展說明:
 ```
 <-- Observable.subscribe(Subscriber) 的內部實現 -->

public Subscription subscribe(Subscriber subscriber) {
    subscriber.onStart();
    // 步驟1中 觀察者  subscriber抽象類複寫的方法,用於初始化工做
    onSubscribe.call(subscriber);
    // 經過該調用,從而回調觀察者中的對應方法從而響應被觀察者生產的事件
    // 從而實現被觀察者調用了觀察者的回調方法 & 由被觀察者向觀察者的事件傳遞,即觀察者模式
    // 同時也看出:Observable只是生產事件,真正的發送事件是在它被訂閱的時候,即當 subscribe() 方法執行時
}

基於事件流的鏈式調用

在實際應用中,會將上述步驟&代碼連在一塊兒,從而更加簡潔、更加優雅,即所謂的 RxJava基於事件流的鏈式調用

// RxJava的鏈式操做
        Observable.create(new ObservableOnSubscribe<Integer>() {
        // 1. 建立被觀察者 & 生產事件
            @Override
            public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
                emitter.onNext(1);
                emitter.onNext(2);
                emitter.onNext(3);
                emitter.onComplete();
            }
        }).subscribe(new Observer<Integer>() {
            // 2. 經過經過訂閱(subscribe)鏈接觀察者和被觀察者
            // 3. 建立觀察者 & 定義響應事件的行爲
            @Override
            public void onSubscribe(Disposable d) {
                Log.d(TAG, "開始採用subscribe鏈接");
            }
            // 默認最早調用複寫的 onSubscribe()

            @Override
            public void onNext(Integer value) {
                Log.d(TAG, "對Next事件"+ value +"做出響應"  );
            }

            @Override
            public void onError(Throwable e) {
                Log.d(TAG, "對Error事件做出響應");
            }

            @Override
            public void onComplete() {
                Log.d(TAG, "對Complete事件做出響應");
            }

        });
    }
}

    注意:總體方法調用順序:觀察者.onSubscribe()> 被觀察者.subscribe()> 觀察者.onNext()>觀察者.onComplete()

隨着程序邏輯的複雜性提升,它依然可以保持簡潔 & 優雅。因此,通常建議使用這種基於事件流的鏈式調用方式實現RxJava
RxJava 2.x 提供了多個函數式接口 ,用於實現簡便式的觀察者模式

 


以consumer爲例,實現觀察者模式 Observable.just(
"hello").subscribe(new Consumer<String>() { // 每次接收到Observable的事件都會調用Consumer.accept() @Override public void accept(String s) throws Exception { System.out.println(s); } }); 實際使用 使用分步實現 package com.paulniu.rxjavademo; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import org.reactivestreams.Subscriber; import org.reactivestreams.Subscription; import io.reactivex.Observable; import io.reactivex.ObservableEmitter; import io.reactivex.ObservableOnSubscribe; import io.reactivex.Observer; import io.reactivex.disposables.Disposable; public class MainActivity extends AppCompatActivity { public static final String TAG = "MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //1.建立觀察者Observable對象 Observable<Integer>observable = Observable.create(new ObservableOnSubscribe<Integer>() { @Override public void subscribe(ObservableEmitter<Integer> e) throws Exception { // 在複寫的subscribe()裏定義須要發送的事件 // 經過 ObservableEmitter類對象產生事件並通知觀察者 // ObservableEmitter類介紹 // a. 定義:事件發射器 // b. 做用:定義須要發送的事件 & 向觀察者發送事件 e.onNext(1); e.onNext(2); e.onNext(3); //完成發送 e.onComplete(); } }); //2:建立觀察者 Observer 並 定義響應事件行爲 Observer<Integer>observer = new Observer<Integer>() { // 經過複寫對應方法來 響應 被觀察者 @Override public void onSubscribe(Disposable d) { // 默認最早調用複寫的 onSubscribe() Log.d(TAG, "開始採用subscribe鏈接"); } @Override public void onNext(Integer value) { Log.d(TAG, "對Next事件"+ value +"做出響應" ); } @Override public void onError(Throwable e) { Log.d(TAG, "對Error事件做出響應"); } @Override public void onComplete() { Log.d(TAG, "對Complete事件做出響應"); } }; //3:經過訂閱(subscribe)鏈接觀察者和被觀察者 observable.subscribe(observer); } } 實驗結果:

 


基於事件流的鏈式調用方式
// RxJava的流式操做 Observable.create(new ObservableOnSubscribe<Integer>() { // 1. 建立被觀察者 & 生產事件 @Override public void subscribe(ObservableEmitter<Integer> emitter) throws Exception { emitter.onNext(1); emitter.onNext(2); emitter.onNext(3); emitter.onComplete(); } }).subscribe(new Observer<Integer>() { // 2. 經過經過訂閱(subscribe)鏈接觀察者和被觀察者 // 3. 建立觀察者 & 定義響應事件的行爲 @Override public void onSubscribe(Disposable d) { Log.d(TAG, "開始採用subscribe鏈接"); } // 默認最早調用複寫的 onSubscribe() @Override public void onNext(Integer value) { Log.d(TAG, "對Next事件"+ value +"做出響應" ); } @Override public void onError(Throwable e) { Log.d(TAG, "對Error事件做出響應"); } @Override public void onComplete() { Log.d(TAG, "對Complete事件做出響應"); } }); 實現效果和上圖同樣 除了此以外,觀察者Observer的subscribe()具備重載方法 public final Disposable subscribe() {} // 表示觀察者不對被觀察者發送的事件做出任何響應(但被觀察者仍是能夠繼續發送事件) public final Disposable subscribe(Consumer<? super T> onNext) {} // 表示觀察者只對被觀察者發送的Next事件做出響應 public final Disposable subscribe(Consumer<? super T> onNext, Consumer<? super Throwable> onError) {} // 表示觀察者只對被觀察者發送的Next事件 & Error事件做出響應 public final Disposable subscribe(Consumer<? super T> onNext, Consumer<? super Throwable> onError, Action onComplete) {} // 表示觀察者只對被觀察者發送的Next事件、Error事件 & Complete事件做出響應 public final Disposable subscribe(Consumer<? super T> onNext, Consumer<? super Throwable> onError, Action onComplete, Consumer<? super Disposable> onSubscribe) {} // 表示觀察者只對被觀察者發送的Next事件、Error事件 、Complete事件 & onSubscribe事件做出響應 public final void subscribe(Observer<? super T> observer) {} // 表示觀察者對被觀察者發送的任何事件都做出響應 可採用 Disposable.dispose() 切斷觀察者 與 被觀察者 之間的鏈接 即觀察者 沒法繼續 接收 被觀察者的事件,但被觀察者仍是能夠繼續發送事件 // 主要在觀察者 Observer中 實現 Observer<Integer> observer = new Observer<Integer>() { // 1. 定義Disposable類變量 private Disposable mDisposable; @Override public void onSubscribe(Disposable d) { Log.d(TAG, "開始採用subscribe鏈接"); // 2. 對Disposable類變量賦值 mDisposable = d; } @Override public void onNext(Integer value) { Log.d(TAG, "對Next事件"+ value +"做出響應" ); if (value == 2) { // 設置在接收到第二個事件後切斷觀察者和被觀察者的鏈接 mDisposable.dispose(); Log.d(TAG, "已經切斷了鏈接:" + mDisposable.isDisposed()); } } @Override public void onError(Throwable e) { Log.d(TAG, "對Error事件做出響應"); } @Override public void onComplete() { Log.d(TAG, "對Complete事件做出響應"); } }; 運行結果

 


RxJava的操做符

做用:建立被觀察者對象(Observable)和發送事件

 


基本建立

完整建立一個被觀察者,同時也是建立被觀察者對象最基本的操做符
create()

具體使用

/ **
   * 1. 經過creat()建立被觀察者 Observable 對象
   */ 
        Observable<Integer> observable = Observable.create(new ObservableOnSubscribe<Integer>() {
          // 傳入參數: OnSubscribe 對象
          // 當 Observable 被訂閱時,OnSubscribe 的 call() 方法會自動被調用,即事件序列就會依照設定依次被觸發
          // 即觀察者會依次調用對應事件的複寫方法從而響應事件
          // 從而實現由被觀察者向觀察者的事件傳遞 & 被觀察者調用了觀察者的回調方法 ,即觀察者模式
/ **
   * 2. 在複寫的subscribe()裏定義須要發送的事件
   */ 
            @Override
            public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
                // 經過 ObservableEmitter類對象 產生 & 發送事件
                // ObservableEmitter類介紹
                    // a. 定義:事件發射器
                    // b. 做用:定義須要發送的事件 & 向觀察者發送事件
                   // 注:建議發送事件前檢查觀察者的isUnsubscribed狀態,以便在沒有觀察者時,讓Observable中止發射數據
                    if (!observer.isUnsubscribed()) {
                           emitter.onNext(1);
                           emitter.onNext(2);
                           emitter.onNext(3);
                }
                emitter.onComplete();
            }
        });

// 至此,一個完整的被觀察者對象(Observable)就建立完畢了。


可是在使用的時候通常採用鏈式方式

// 1. 經過creat()建立被觀察者對象
        Observable.create(new ObservableOnSubscribe<Integer>() {

            // 2. 在複寫的subscribe()裏定義須要發送的事件
            @Override
            public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {

                    emitter.onNext(1);
                    emitter.onNext(2);
                    emitter.onNext(3);

                emitter.onComplete();
            }  // 至此,一個被觀察者對象(Observable)就建立完畢
        }).subscribe(new Observer<Integer>() {
            // 如下步驟僅爲展現一個完整demo,能夠忽略
            // 3. 經過經過訂閱(subscribe)鏈接觀察者和被觀察者
            // 4. 建立觀察者 & 定義響應事件的行爲
            @Override
            public void onSubscribe(Disposable d) {
                Log.d(TAG, "開始採用subscribe鏈接");
            }
            // 默認最早調用複寫的 onSubscribe()

            @Override
            public void onNext(Integer value) {
                Log.d(TAG, "接收到了事件"+ value  );
            }

            @Override
            public void onError(Throwable e) {
                Log.d(TAG, "對Error事件做出響應");
            }

            @Override
            public void onComplete() {
                Log.d(TAG, "對Complete事件做出響應");
            }

        });
    }


結果

 


just()

快速建立1個被觀察者對象(Observable),直接發送 傳入的事件

    最多隻能傳10個參數

具體使用:

// 1. 建立時傳入整型一、二、三、4
        // 在建立後就會發送這些對象,至關於執行了onNext(1)、onNext(2)、onNext(3)、onNext(4)
        Observable.just(1, 2, 3,4)   
            // 至此,一個Observable對象建立完畢,如下步驟僅爲展現一個完整demo,能夠忽略
            // 2. 經過經過訂閱(subscribe)鏈接觀察者和被觀察者
            // 3. 建立觀察者 & 定義響應事件的行爲
         .subscribe(new Observer<Integer>() {

            @Override
            public void onSubscribe(Disposable d) {
                Log.d(TAG, "開始採用subscribe鏈接");
            }
            // 默認最早調用複寫的 onSubscribe()

            @Override
            public void onNext(Integer value) {
                Log.d(TAG, "接收到了事件"+ value  );
            }

            @Override
            public void onError(Throwable e) {
                Log.d(TAG, "對Error事件做出響應");
            }

            @Override
            public void onComplete() {
                Log.d(TAG, "對Complete事件做出響應");
            }

        });
    }


結果:

 


fromArray()

快速建立1個被觀察者對象(Observable),直接發送 傳入的數組數據

    會將數組中的數據轉換爲Observable對象

具體使用:

// 1. 設置須要傳入的數組
     Integer[] items = { 0, 1, 2, 3, 4 };

        // 2. 建立被觀察者對象(Observable)時傳入數組
        // 在建立後就會將該數組轉換成Observable & 發送該對象中的全部數據
        Observable.fromArray(items) 
        .subscribe(new Observer<Integer>() {
            @Override
            public void onSubscribe(Disposable d) {
                Log.d(TAG, "開始採用subscribe鏈接");
            }

            @Override
            public void onNext(Integer value) {
                Log.d(TAG, "接收到了事件"+ value  );
            }

            @Override
            public void onError(Throwable e) {
                Log.d(TAG, "對Error事件做出響應");
            }

            @Override
            public void onComplete() {
                Log.d(TAG, "對Complete事件做出響應");
            }

        });
    }

// 注:
// 可發送10個以上參數
// 若直接傳遞一個list集合進去,不然會直接把list當作一個數據元素髮送

/*
  * 數組遍歷
  **/
        // 1. 設置須要傳入的數組
        Integer[] items = { 0, 1, 2, 3, 4 };

        // 2. 建立被觀察者對象(Observable)時傳入數組
        // 在建立後就會將該數組轉換成Observable & 發送該對象中的全部數據
        Observable.fromArray(items)
                .subscribe(new Observer<Integer>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        Log.d(TAG, "數組遍歷");
                    }

                    @Override
                    public void onNext(Integer value) {
                        Log.d(TAG, "數組中的元素 = "+ value  );
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.d(TAG, "對Error事件做出響應");
                    }

                    @Override
                    public void onComplete() {
                        Log.d(TAG, "遍歷結束");
                    }

                });


結果:

 


 


fromIterable()

快速建立1個被觀察者對象(Observable),直接發送 傳入的集合List數據

    會將數組中的數據轉換爲Observable對象

具體實現

/*
 * 快速發送集合
 **/
// 1. 設置一個集合
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);

// 2. 經過fromIterable()將集合中的對象 / 數據發送出去
        Observable.fromIterable(list)
                .subscribe(new Observer<Integer>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        Log.d(TAG, "開始採用subscribe鏈接");
                    }

                    @Override
                    public void onNext(Integer value) {
                        Log.d(TAG, "接收到了事件"+ value  );
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.d(TAG, "對Error事件做出響應");
                    }

                    @Override
                    public void onComplete() {
                        Log.d(TAG, "對Complete事件做出響應");
                    }
                });


/*
 * 集合遍歷
 **/
        // 1. 設置一個集合
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);

        // 2. 經過fromIterable()將集合中的對象 / 數據發送出去
        Observable.fromIterable(list)
                .subscribe(new Observer<Integer>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        Log.d(TAG, "集合遍歷");
                    }

                    @Override
                    public void onNext(Integer value) {
                        Log.d(TAG, "集合中的數據元素 = "+ value  );
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.d(TAG, "對Error事件做出響應");
                    }

                    @Override
                    public void onComplete() {
                        Log.d(TAG, "遍歷結束");
                    }
                });


結果:

 


 



    其餘

// 下列方法通常用於測試使用

<-- empty()  -->
// 該方法建立的被觀察者對象發送事件的特色:僅發送Complete事件,直接通知完成
Observable observable1=Observable.empty(); 
// 即觀察者接收後會直接調用onCompleted()

<-- error()  -->
// 該方法建立的被觀察者對象發送事件的特色:僅發送Error事件,直接通知異常
// 可自定義異常
Observable observable2=Observable.error(new RuntimeException())
// 即觀察者接收後會直接調用onError()

<-- never()  -->
// 該方法建立的被觀察者對象發送事件的特色:不發送任何事件
Observable observable3=Observable.never();
// 即觀察者接收後什麼都不調用

延遲建立

使用場景:
1. 定時操做:在通過了x秒後,須要自動執行y操做
2. 週期性操做:每隔x秒後,須要自動執行y操做
defer()

    做用: 直到有觀察者(Observer )訂閱時,才動態建立被觀察者對象(Observable) & 發送事件

    經過 Observable工廠方法建立被觀察者對象(Observable)
    每次訂閱後,都會獲得一個剛建立的最新的Observable對象,這能夠確保Observable對象裏的數據是最新的
    具體使用:

<-- 1. 第1次對i賦值 ->>
        Integer i = 10;

        // 2. 經過defer 定義被觀察者對象
        // 注:此時被觀察者對象還沒建立
        Observable<Integer> observable = Observable.defer(new Callable<ObservableSource<? extends Integer>>() {
            @Override
            public ObservableSource<? extends Integer> call() throws Exception {
                return Observable.just(i);
            }
        });

        <-- 2. 第2次對i賦值 ->>
        i = 15;

        <-- 3. 觀察者開始訂閱 ->>
        // 注:此時,纔會調用defer()建立被觀察者對象(Observable)
        observable.subscribe(new Observer<Integer>() {

            @Override
            public void onSubscribe(Disposable d) {
                Log.d(TAG, "開始採用subscribe鏈接");
            }

            @Override
            public void onNext(Integer value) {
                Log.d(TAG, "接收到的整數是"+ value  );
            }

            @Override
            public void onError(Throwable e) {
                Log.d(TAG, "對Error事件做出響應");
            }

            @Override
            public void onComplete() {
                Log.d(TAG, "對Complete事件做出響應");
            }
        });


結果:
由於是在訂閱時才建立,因此i值會取第2次的賦值

 


timer()

    快速建立1個被觀察者對象(Observable)
    發送事件的特色:延遲指定時間後,發送1個數值0(Long類型)

    本質 = 延遲指定時間後,調用一次 onNext(0)

具體使用:

// 該例子 = 延遲2s後,發送一個long類型數值
        Observable.timer(2, TimeUnit.SECONDS) 
                  .subscribe(new Observer<Long>() {
            @Override
            public void onSubscribe(Disposable d) {
                Log.d(TAG, "開始採用subscribe鏈接");
            }

            @Override
            public void onNext(Long value) {
                Log.d(TAG, "接收到了事件"+ value  );
            }

            @Override
            public void onError(Throwable e) {
                Log.d(TAG, "對Error事件做出響應");
            }

            @Override
            public void onComplete() {
                Log.d(TAG, "對Complete事件做出響應");
            }

        });

// 注:timer操做符默認運行在一個新線程上
// 也可自定義線程調度器(第3個參數):timer(long,TimeUnit,Scheduler)


結果

 


interval()

    快速建立1個被觀察者對象(Observable)
    發送事件的特色:每隔指定時間 就發送 事件

    發送的事件序列 = 從0開始、無限遞增1的的整數序列

具體實現

// 參數說明:
        // 參數1 = 第1次延遲時間;
        // 參數2 = 間隔時間數字;
        // 參數3 = 時間單位;
        Observable.interval(3,1,TimeUnit.SECONDS)
                // 該例子發送的事件序列特色:延遲3s後發送事件,每隔1秒產生1個數字(從0開始遞增1,無限個)
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        Log.d(TAG, "開始採用subscribe鏈接");
                    }
                    // 默認最早調用複寫的 onSubscribe()

                    @Override
                    public void onNext(Long value) {
                        Log.d(TAG, "接收到了事件"+ value  );
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.d(TAG, "對Error事件做出響應");
                    }

                    @Override
                    public void onComplete() {
                        Log.d(TAG, "對Complete事件做出響應");
                    }

                });

// 注:interval默認在computation調度器上執行
// 也可自定義指定線程調度器(第3個參數):interval(long,TimeUnit,Scheduler)


結果:

 


intervalRange()

    快速建立1個被觀察者對象(Observable)
    發送事件的特色:每隔指定時間 就發送 事件,可指定發送的數據的數量

    a. 發送的事件序列 = 從0開始、無限遞增1的的整數序列
    b. 做用相似於interval(),但可指定發送的數據的數量

具體實現

// 參數說明:
        // 參數1 = 事件序列起始點;
        // 參數2 = 事件數量;
        // 參數3 = 第1次事件延遲發送時間;
        // 參數4 = 間隔時間數字;
        // 參數5 = 時間單位
        Observable.intervalRange(3,10,2, 1, TimeUnit.SECONDS)
                // 該例子發送的事件序列特色:
                // 1. 從3開始,一共發送10個事件;
                // 2. 第1次延遲2s發送,以後每隔2秒產生1個數字(從0開始遞增1,無限個)
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        Log.d(TAG, "開始採用subscribe鏈接");
                    }
                    // 默認最早調用複寫的 onSubscribe()

                    @Override
                    public void onNext(Long value) {
                        Log.d(TAG, "接收到了事件"+ value  );
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.d(TAG, "對Error事件做出響應");
                    }

                    @Override
                    public void onComplete() {
                        Log.d(TAG, "對Complete事件做出響應");
                    }

                });


結果:

 


range()

    快速建立1個被觀察者對象(Observable)
    發送事件的特色:連續發送 1個事件序列,可指定範圍

    a. 發送的事件序列 = 從0開始、無限遞增1的的整數序列
    b. 做用相似於intervalRange(),但區別在於:無延遲發送事件

具體實現:

// 參數說明:
        // 參數1 = 事件序列起始點;
        // 參數2 = 事件數量;
        // 注:若設置爲負數,則會拋出異常
        Observable.range(3,10)
                // 該例子發送的事件序列特色:從3開始發送,每次發送事件遞增1,一共發送10個事件
                .subscribe(new Observer<Integer>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        Log.d(TAG, "開始採用subscribe鏈接");
                    }
                    // 默認最早調用複寫的 onSubscribe()

                    @Override
                    public void onNext(Integer value) {
                        Log.d(TAG, "接收到了事件"+ value  );
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.d(TAG, "對Error事件做出響應");
                    }

                    @Override
                    public void onComplete() {
                        Log.d(TAG, "對Complete事件做出響應");
                    }

                });


結果:

 


rangeLong()

    做用:相似於range(),區別在於該方法支持數據類型 = Long
    具體使用與range()相似,此處不做過多描述

最後的總結:

 



一個例子:
1. 添加依賴

// Android 支持 Rxjava
    // 此處必定要注意使用RxJava2的版本
    compile 'io.reactivex.rxjava2:rxjava:2.0.1'
    compile 'io.reactivex.rxjava2:rxandroid:2.0.1'

    // Android 支持 Retrofit
    compile 'com.squareup.retrofit2:retrofit:2.1.0'

    // 銜接 Retrofit & RxJava
    // 此處必定要注意使用RxJava2的版本
    compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'

    // 支持Gson解析
    compile 'com.squareup.retrofit2:converter-gson:2.1.0'

    compile 'com.orhanobut:logger:2.1.1'


    建立實體類

package com.paulniu.rxjavademo;

import java.util.List;

/**
 * Created by niupule on 2017/12/30.
 */

public class MovieBean {


    /**
     * count : 20
     * start : 0
     * total : 25
     * subjects : [{"rating":{"max":10,"average":4.9,"stars":"25","min":0},"genres":["喜劇"],"title":"妖鈴鈴","casts":[{"alt":"https://movie.douban.com/celebrity/1127819/","avatars":{"small":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p45539.webp","large":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p45539.webp","medium":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p45539.webp"},"name":"吳君如","id":"1127819"},{"alt":"https://movie.douban.com/celebrity/1325700/","avatars":{"small":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1356510694.28.webp","large":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1356510694.28.webp","medium":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1356510694.28.webp"},"name":"沈騰","id":"1325700"},{"alt":"https://movie.douban.com/celebrity/1317663/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p40756.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p40756.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p40756.webp"},"name":"岳雲鵬","id":"1317663"}],"collect_count":10595,"original_title":"妖鈴鈴","subtype":"movie","directors":[{"alt":"https://movie.douban.com/celebrity/1127819/","avatars":{"small":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p45539.webp","large":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p45539.webp","medium":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p45539.webp"},"name":"吳君如","id":"1127819"}],"year":"2017","images":{"small":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2506825662.webp","large":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2506825662.webp","medium":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2506825662.webp"},"alt":"https://movie.douban.com/subject/26966580/","id":"26966580"},{"rating":{"max":10,"average":0,"stars":"00","min":0},"genres":["兒童","動畫","奇幻"],"title":"小貓巴克裏","casts":[{"alt":"https://movie.douban.com/celebrity/1386018/","avatars":{"small":"https://img1.doubanio.com/f/movie/ca527386eb8c4e325611e22dfcb04cc116d6b423/pics/movie/celebrity-default-small.png","large":"https://img3.doubanio.com/f/movie/63acc16ca6309ef191f0378faf793d1096a3e606/pics/movie/celebrity-default-large.png","medium":"https://img1.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png"},"name":"林佑俽","id":"1386018"},{"alt":"https://movie.douban.com/celebrity/1386019/","avatars":{"small":"https://img1.doubanio.com/f/movie/ca527386eb8c4e325611e22dfcb04cc116d6b423/pics/movie/celebrity-default-small.png","large":"https://img3.doubanio.com/f/movie/63acc16ca6309ef191f0378faf793d1096a3e606/pics/movie/celebrity-default-large.png","medium":"https://img1.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png"},"name":"郭馨雅","id":"1386019"},{"alt":"https://movie.douban.com/celebrity/1275593/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p35136.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p35136.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p35136.webp"},"name":"屈中恆","id":"1275593"}],"collect_count":84,"original_title":"小貓巴克裏","subtype":"movie","directors":[{"alt":"https://movie.douban.com/celebrity/1372804/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1512959486.04.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1512959486.04.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1512959486.04.webp"},"name":"邱立偉","id":"1372804"}],"year":"2017","images":{"small":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2508948724.webp","large":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2508948724.webp","medium":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2508948724.webp"},"alt":"https://movie.douban.com/subject/26887161/","id":"26887161"},{"rating":{"max":10,"average":7.8,"stars":"40","min":0},"genres":["劇情","歷史","戰爭"],"title":"芳華","casts":[{"alt":"https://movie.douban.com/celebrity/1276105/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1403053084.22.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1403053084.22.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1403053084.22.webp"},"name":"黃軒","id":"1276105"},{"alt":"https://movie.douban.com/celebrity/1366978/","avatars":{"small":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1512871367.97.webp","large":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1512871367.97.webp","medium":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1512871367.97.webp"},"name":"苗苗","id":"1366978"},{"alt":"https://movie.douban.com/celebrity/1357009/","avatars":{"small":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1504775118.88.webp","large":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1504775118.88.webp","medium":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1504775118.88.webp"},"name":"鍾楚曦","id":"1357009"}],"collect_count":226595,"original_title":"芳華","subtype":"movie","directors":[{"alt":"https://movie.douban.com/celebrity/1274255/","avatars":{"small":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p45667.webp","large":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p45667.webp","medium":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p45667.webp"},"name":"馮小剛","id":"1274255"}],"year":"2017","images":{"small":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2507227732.webp","large":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2507227732.webp","medium":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2507227732.webp"},"alt":"https://movie.douban.com/subject/26862829/","id":"26862829"},{"rating":{"max":10,"average":6.2,"stars":"35","min":0},"genres":["喜劇","愛情","奇幻"],"title":"二代妖精之此生有幸","casts":[{"alt":"https://movie.douban.com/celebrity/1275721/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p36925.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p36925.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p36925.webp"},"name":"馮紹峯","id":"1275721"},{"alt":"https://movie.douban.com/celebrity/1049732/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1513067217.13.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1513067217.13.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1513067217.13.webp"},"name":"劉亦菲","id":"1049732"},{"alt":"https://movie.douban.com/celebrity/1275178/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p37722.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p37722.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p37722.webp"},"name":"李光潔","id":"1275178"}],"collect_count":10818,"original_title":"二代妖精之此生有幸","subtype":"movie","directors":[{"alt":"https://movie.douban.com/celebrity/1331182/","avatars":{"small":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1426816047.48.webp","large":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1426816047.48.webp","medium":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1426816047.48.webp"},"name":"肖洋","id":"1331182"}],"year":"2017","images":{"small":"https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2507022339.webp","large":"https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2507022339.webp","medium":"https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2507022339.webp"},"alt":"https://movie.douban.com/subject/26797419/","id":"26797419"},{"rating":{"max":10,"average":6.1,"stars":"30","min":0},"genres":["喜劇","愛情"],"title":"前任3:再見前任","casts":[{"alt":"https://movie.douban.com/celebrity/1275667/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p14025.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p14025.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p14025.webp"},"name":"韓庚","id":"1275667"},{"alt":"https://movie.douban.com/celebrity/1275564/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1366015827.84.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1366015827.84.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1366015827.84.webp"},"name":"鄭愷","id":"1275564"},{"alt":"https://movie.douban.com/celebrity/1342252/","avatars":{"small":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1408089064.98.webp","large":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1408089064.98.webp","medium":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1408089064.98.webp"},"name":"於文文","id":"1342252"}],"collect_count":5464,"original_title":"前任3:再見前任","subtype":"movie","directors":[{"alt":"https://movie.douban.com/celebrity/1332171/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1391439365.41.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1391439365.41.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1391439365.41.webp"},"name":"田羽生","id":"1332171"}],"year":"2017","images":{"small":"https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2508926717.webp","large":"https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2508926717.webp","medium":"https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2508926717.webp"},"alt":"https://movie.douban.com/subject/26662193/","id":"26662193"},{"rating":{"max":10,"average":5.4,"stars":"30","min":0},"genres":["劇情","奇幻"],"title":"解憂雜貨店","casts":[{"alt":"https://movie.douban.com/celebrity/1339594/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1503377320.23.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1503377320.23.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1503377320.23.webp"},"name":"王俊凱","id":"1339594"},{"alt":"https://movie.douban.com/celebrity/1325127/","avatars":{"small":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1492095415.48.webp","large":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1492095415.48.webp","medium":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1492095415.48.webp"},"name":"迪麗熱巴","id":"1325127"},{"alt":"https://movie.douban.com/celebrity/1330472/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1385100761.5.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1385100761.5.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1385100761.5.webp"},"name":"董子健","id":"1330472"}],"collect_count":17076,"original_title":"解憂雜貨店","subtype":"movie","directors":[{"alt":"https://movie.douban.com/celebrity/1316056/","avatars":{"small":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p34888.webp","large":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p34888.webp","medium":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p34888.webp"},"name":"韓傑","id":"1316056"}],"year":"2017","images":{"small":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2503544593.webp","large":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2503544593.webp","medium":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2503544593.webp"},"alt":"https://movie.douban.com/subject/26654146/","id":"26654146"},{"rating":{"max":10,"average":6.9,"stars":"35","min":0},"genres":["劇情","懸疑","奇幻"],"title":"妖貓傳","casts":[{"alt":"https://movie.douban.com/celebrity/1276105/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1403053084.22.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1403053084.22.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1403053084.22.webp"},"name":"黃軒","id":"1276105"},{"alt":"https://movie.douban.com/celebrity/1315737/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p50940.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p50940.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p50940.webp"},"name":"染谷將太","id":"1315737"},{"alt":"https://movie.douban.com/celebrity/1274494/","avatars":{"small":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1510497293.38.webp","large":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1510497293.38.webp","medium":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1510497293.38.webp"},"name":"張雨綺","id":"1274494"}],"collect_count":147235,"original_title":"妖貓傳","subtype":"movie","directors":[{"alt":"https://movie.douban.com/celebrity/1023040/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1451727734.81.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1451727734.81.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1451727734.81.webp"},"name":"陳凱歌","id":"1023040"}],"year":"2017","images":{"small":"https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2507024497.webp","large":"https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2507024497.webp","medium":"https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2507024497.webp"},"alt":"https://movie.douban.com/subject/5350027/","id":"5350027"},{"rating":{"max":10,"average":9.1,"stars":"45","min":0},"genres":["喜劇","動畫","冒險"],"title":"尋夢環遊記","casts":[{"alt":"https://movie.douban.com/celebrity/1370411/","avatars":{"small":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1489594517.9.webp","large":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1489594517.9.webp","medium":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1489594517.9.webp"},"name":"安東尼·岡薩雷斯","id":"1370411"},{"alt":"https://movie.douban.com/celebrity/1041009/","avatars":{"small":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1510634028.69.webp","large":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1510634028.69.webp","medium":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1510634028.69.webp"},"name":"蓋爾·加西亞·貝納爾","id":"1041009"},{"alt":"https://movie.douban.com/celebrity/1036383/","avatars":{"small":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1416762292.89.webp","large":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1416762292.89.webp","medium":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1416762292.89.webp"},"name":"本傑明·布拉特","id":"1036383"}],"collect_count":330788,"original_title":"Coco","subtype":"movie","directors":[{"alt":"https://movie.douban.com/celebrity/1022678/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p13830.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p13830.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p13830.webp"},"name":"李·昂克里奇","id":"1022678"},{"alt":"https://movie.douban.com/celebrity/1370425/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1497195148.21.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1497195148.21.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1497195148.21.webp"},"name":"阿德里安·莫利納","id":"1370425"}],"year":"2017","images":{"small":"https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2503997609.webp","large":"https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2503997609.webp","medium":"https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2503997609.webp"},"alt":"https://movie.douban.com/subject/20495023/","id":"20495023"},{"rating":{"max":10,"average":0,"stars":"00","min":0},"genres":["兒童","動畫","奇幻"],"title":"咕嚕咕嚕美人魚2","casts":[{"alt":"https://movie.douban.com/celebrity/1386282/","avatars":{"small":"https://img1.doubanio.com/f/movie/ca527386eb8c4e325611e22dfcb04cc116d6b423/pics/movie/celebrity-default-small.png","large":"https://img3.doubanio.com/f/movie/63acc16ca6309ef191f0378faf793d1096a3e606/pics/movie/celebrity-default-large.png","medium":"https://img1.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png"},"name":"丁瑩瑩","id":"1386282"},{"alt":"https://movie.douban.com/celebrity/1356706/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1481650266.82.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1481650266.82.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1481650266.82.webp"},"name":"楊進","id":"1356706"},{"alt":"https://movie.douban.com/celebrity/1348773/","avatars":{"small":"https://img1.doubanio.com/f/movie/ca527386eb8c4e325611e22dfcb04cc116d6b423/pics/movie/celebrity-default-small.png","large":"https://img3.doubanio.com/f/movie/63acc16ca6309ef191f0378faf793d1096a3e606/pics/movie/celebrity-default-large.png","medium":"https://img1.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png"},"name":"盧瑤","id":"1348773"}],"collect_count":52,"original_title":"咕嚕咕嚕美人魚2","subtype":"movie","directors":[{"alt":"https://movie.douban.com/celebrity/1340240/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1483596470.52.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1483596470.52.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1483596470.52.webp"},"name":"楊廣福","id":"1340240"}],"year":"2017","images":{"small":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2508071436.webp","large":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2508071436.webp","medium":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2508071436.webp"},"alt":"https://movie.douban.com/subject/27193475/","id":"27193475"},{"rating":{"max":10,"average":8.3,"stars":"45","min":0},"genres":["喜劇","動畫","家庭"],"title":"帕丁頓熊2","casts":[{"alt":"https://movie.douban.com/celebrity/1025149/","avatars":{"small":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1397997449.99.webp","large":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1397997449.99.webp","medium":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1397997449.99.webp"},"name":"本·衛肖","id":"1025149"},{"alt":"https://movie.douban.com/celebrity/1003493/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1871.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1871.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1871.webp"},"name":"休·格蘭特","id":"1003493"},{"alt":"https://movie.douban.com/celebrity/1041224/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1416053013.86.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1416053013.86.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1416053013.86.webp"},"name":"休·博內威利","id":"1041224"}],"collect_count":54486,"original_title":"Paddington 2","subtype":"movie","directors":[{"alt":"https://movie.douban.com/celebrity/1313689/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1425285993.52.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1425285993.52.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1425285993.52.webp"},"name":"保羅·金","id":"1313689"}],"year":"2017","images":{"small":"https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2506466229.webp","large":"https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2506466229.webp","medium":"https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2506466229.webp"},"alt":"https://movie.douban.com/subject/26340419/","id":"26340419"},{"rating":{"max":10,"average":6.2,"stars":"30","min":0},"genres":["動做","犯罪","懸疑"],"title":"心理罪之城市之光","casts":[{"alt":"https://movie.douban.com/celebrity/1274235/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p805.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p805.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p805.webp"},"name":"鄧超","id":"1274235"},{"alt":"https://movie.douban.com/celebrity/1259866/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p21006.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p21006.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p21006.webp"},"name":"阮經天","id":"1259866"},{"alt":"https://movie.douban.com/celebrity/1274533/","avatars":{"small":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p35797.webp","large":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p35797.webp","medium":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p35797.webp"},"name":"劉詩詩","id":"1274533"}],"collect_count":30498,"original_title":"心理罪之城市之光","subtype":"movie","directors":[{"alt":"https://movie.douban.com/celebrity/1317195/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p38956.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p38956.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p38956.webp"},"name":"徐紀周","id":"1317195"}],"year":"2017","images":{"small":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2506228506.webp","large":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2506228506.webp","medium":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2506228506.webp"},"alt":"https://movie.douban.com/subject/26774722/","id":"26774722"},{"rating":{"max":10,"average":5,"stars":"25","min":0},"genres":["劇情","動做","科幻"],"title":"機器之血","casts":[{"alt":"https://movie.douban.com/celebrity/1054531/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p694.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p694.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p694.webp"},"name":"成龍","id":"1054531"},{"alt":"https://movie.douban.com/celebrity/1274317/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p3083.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p3083.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p3083.webp"},"name":"羅志祥","id":"1274317"},{"alt":"https://movie.douban.com/celebrity/1336314/","avatars":{"small":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1499104651.08.webp","large":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1499104651.08.webp","medium":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1499104651.08.webp"},"name":"歐陽娜娜","id":"1336314"}],"collect_count":10002,"original_title":"機器之血","subtype":"movie","directors":[{"alt":"https://movie.douban.com/celebrity/1324053/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1495169894.72.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1495169894.72.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1495169894.72.webp"},"name":"張立嘉","id":"1324053"}],"year":"2017","images":{"small":"https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2505785547.webp","large":"https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2505785547.webp","medium":"https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2505785547.webp"},"alt":"https://movie.douban.com/subject/26729868/","id":"26729868"},{"rating":{"max":10,"average":8.6,"stars":"45","min":0},"genres":["劇情","傳記","動畫"],"title":"至愛梵高·星空之謎","casts":[{"alt":"https://movie.douban.com/celebrity/1314461/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p43221.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p43221.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p43221.webp"},"name":"道格拉斯·布斯","id":"1314461"},{"alt":"https://movie.douban.com/celebrity/1376200/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1498554583.31.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1498554583.31.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1498554583.31.webp"},"name":"羅伯特·古拉奇克","id":"1376200"},{"alt":"https://movie.douban.com/celebrity/1275043/","avatars":{"small":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1360941730.7.webp","large":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1360941730.7.webp","medium":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1360941730.7.webp"},"name":"埃莉諾·湯姆林森","id":"1275043"}],"collect_count":81929,"original_title":"Loving Vincent","subtype":"movie","directors":[{"alt":"https://movie.douban.com/celebrity/1326282/","avatars":{"small":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1393488180.49.webp","large":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1393488180.49.webp","medium":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1393488180.49.webp"},"name":"多洛塔·科別拉","id":"1326282"},{"alt":"https://movie.douban.com/celebrity/1306202/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1498554460.64.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1498554460.64.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1498554460.64.webp"},"name":"休·韋爾什曼","id":"1306202"}],"year":"2017","images":{"small":"https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2506935748.webp","large":"https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2506935748.webp","medium":"https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2506935748.webp"},"alt":"https://movie.douban.com/subject/25837262/","id":"25837262"},{"rating":{"max":10,"average":4.8,"stars":"25","min":0},"genres":["動做","奇幻"],"title":"奇門遁甲","casts":[{"alt":"https://movie.douban.com/celebrity/1324043/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1490342249.11.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1490342249.11.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1490342249.11.webp"},"name":"大鵬","id":"1324043"},{"alt":"https://movie.douban.com/celebrity/1315861/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1368598869.24.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1368598869.24.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1368598869.24.webp"},"name":"倪妮","id":"1315861"},{"alt":"https://movie.douban.com/celebrity/1274292/","avatars":{"small":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p10299.webp","large":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p10299.webp","medium":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p10299.webp"},"name":"李治廷","id":"1274292"}],"collect_count":22624,"original_title":"奇門遁甲","subtype":"movie","directors":[{"alt":"https://movie.douban.com/celebrity/1275026/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p9332.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p9332.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p9332.webp"},"name":"袁和平","id":"1275026"}],"year":"2017","images":{"small":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2507566212.webp","large":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2507566212.webp","medium":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2507566212.webp"},"alt":"https://movie.douban.com/subject/26661191/","id":"26661191"},{"rating":{"max":10,"average":8.6,"stars":"45","min":0},"genres":["劇情","傳記","歷史"],"title":"至暗時刻","casts":[{"alt":"https://movie.douban.com/celebrity/1010507/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p33896.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p33896.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p33896.webp"},"name":"加里·奧德曼","id":"1010507"},{"alt":"https://movie.douban.com/celebrity/1021997/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1512573236.06.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1512573236.06.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1512573236.06.webp"},"name":"克里斯汀·斯科特·托馬斯","id":"1021997"},{"alt":"https://movie.douban.com/celebrity/1318674/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1426508419.1.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1426508419.1.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1426508419.1.webp"},"name":"莉莉·詹姆斯","id":"1318674"}],"collect_count":50108,"original_title":"Darkest Hour","subtype":"movie","directors":[{"alt":"https://movie.douban.com/celebrity/1275041/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1424001982.32.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1424001982.32.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1424001982.32.webp"},"name":"喬·賴特","id":"1275041"}],"year":"2017","images":{"small":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2504277551.webp","large":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2504277551.webp","medium":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2504277551.webp"},"alt":"https://movie.douban.com/subject/26761416/","id":"26761416"},{"rating":{"max":10,"average":5.5,"stars":"30","min":0},"genres":["劇情"],"title":"咖啡風暴","casts":[{"alt":"https://movie.douban.com/celebrity/1083010/","avatars":{"small":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p21789.webp","large":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p21789.webp","medium":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p21789.webp"},"name":"恩尼奧·凡塔斯蒂基尼","id":"1083010"},{"alt":"https://movie.douban.com/celebrity/1315585/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1380440677.6.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1380440677.6.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1380440677.6.webp"},"name":"蘆芳生","id":"1315585"},{"alt":"https://movie.douban.com/celebrity/1312976/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p38291.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p38291.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p38291.webp"},"name":"譚卓","id":"1312976"}],"collect_count":91,"original_title":"Caffè","subtype":"movie","directors":[{"alt":"https://movie.douban.com/celebrity/1010292/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p24213.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p24213.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p24213.webp"},"name":"克里斯蒂亞諾·博爾託內","id":"1010292"}],"year":"2016","images":{"small":"https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2508258787.webp","large":"https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2508258787.webp","medium":"https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2508258787.webp"},"alt":"https://movie.douban.com/subject/26649225/","id":"26649225"},{"rating":{"max":10,"average":6.8,"stars":"35","min":0},"genres":["喜劇","家庭"],"title":"聖誕奇妙公司","casts":[{"alt":"https://movie.douban.com/celebrity/1009332/","avatars":{"small":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p39998.webp","large":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p39998.webp","medium":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p39998.webp"},"name":"格什菲·法拉哈尼","id":"1009332"},{"alt":"https://movie.douban.com/celebrity/1004860/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1973.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1973.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1973.webp"},"name":"奧黛麗·塔圖","id":"1004860"},{"alt":"https://movie.douban.com/celebrity/1036749/","avatars":{"small":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p4917.webp","large":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p4917.webp","medium":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p4917.webp"},"name":"阿蘭·夏巴","id":"1036749"}],"collect_count":1654,"original_title":"Santa & Cie","subtype":"movie","directors":[{"alt":"https://movie.douban.com/celebrity/1036749/","avatars":{"small":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p4917.webp","large":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p4917.webp","medium":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p4917.webp"},"name":"阿蘭·夏巴","id":"1036749"}],"year":"2017","images":{"small":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2505778884.webp","large":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2505778884.webp","medium":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2505778884.webp"},"alt":"https://movie.douban.com/subject/27011205/","id":"27011205"},{"rating":{"max":10,"average":4.8,"stars":"25","min":0},"genres":["喜劇","犯罪"],"title":"這就是命","casts":[{"alt":"https://movie.douban.com/celebrity/1317139/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1371453539.51.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1371453539.51.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1371453539.51.webp"},"name":"王迅","id":"1317139"},{"alt":"https://movie.douban.com/celebrity/1002862/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1161.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1161.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1161.webp"},"name":"曾志偉","id":"1002862"},{"alt":"https://movie.douban.com/celebrity/1340702/","avatars":{"small":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1402633807.18.webp","large":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1402633807.18.webp","medium":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1402633807.18.webp"},"name":"梁超","id":"1340702"}],"collect_count":1432,"original_title":"這就是命","subtype":"movie","directors":[{"alt":"https://movie.douban.com/celebrity/1335838/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1507906324.74.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1507906324.74.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1507906324.74.webp"},"name":"王丹","id":"1335838"}],"year":"2017","images":{"small":"https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2506823369.webp","large":"https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2506823369.webp","medium":"https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2506823369.webp"},"alt":"https://movie.douban.com/subject/27037539/","id":"27037539"},{"rating":{"max":10,"average":6.6,"stars":"35","min":0},"genres":["動做","科幻","奇幻"],"title":"正義聯盟","casts":[{"alt":"https://movie.douban.com/celebrity/1054417/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p7622.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p7622.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p7622.webp"},"name":"本·阿弗萊克","id":"1054417"},{"alt":"https://movie.douban.com/celebrity/1044713/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1371934661.95.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1371934661.95.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1371934661.95.webp"},"name":"亨利·卡維爾","id":"1044713"},{"alt":"https://movie.douban.com/celebrity/1044996/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1467908677.92.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1467908677.92.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1467908677.92.webp"},"name":"蓋爾·加朵","id":"1044996"}],"collect_count":121703,"original_title":"Justice League","subtype":"movie","directors":[{"alt":"https://movie.douban.com/celebrity/1031904/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p23346.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p23346.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p23346.webp"},"name":"扎克·施奈德","id":"1031904"}],"year":"2017","images":{"small":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2504027804.webp","large":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2504027804.webp","medium":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2504027804.webp"},"alt":"https://movie.douban.com/subject/2158490/","id":"2158490"},{"rating":{"max":10,"average":6.2,"stars":"30","min":0},"genres":["驚悚","冒險"],"title":"鯊海","casts":[{"alt":"https://movie.douban.com/celebrity/1000013/","avatars":{"small":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p23017.webp","large":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p23017.webp","medium":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p23017.webp"},"name":"曼迪·摩爾","id":"1000013"},{"alt":"https://movie.douban.com/celebrity/1232340/","avatars":{"small":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p46657.webp","large":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p46657.webp","medium":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p46657.webp"},"name":"克萊爾·霍爾特","id":"1232340"},{"alt":"https://movie.douban.com/celebrity/1050080/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p11804.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p11804.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p11804.webp"},"name":"克里斯·J.約翰遜","id":"1050080"}],"collect_count":11033,"original_title":"47 Meters Down","subtype":"movie","directors":[{"alt":"https://movie.douban.com/celebrity/1019388/","avatars":{"small":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1498455992.67.webp","large":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1498455992.67.webp","medium":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1498455992.67.webp"},"name":"約翰內斯·羅伯茨","id":"1019388"}],"year":"2016","images":{"small":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2504892832.webp","large":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2504892832.webp","medium":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2504892832.webp"},"alt":"https://movie.douban.com/subject/26845362/","id":"26845362"}]
     * title : 正在上映的電影-北京
     */

    private int count;
    private int start;
    private int total;
    private String title;
    private List<SubjectsBean> subjects;

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public int getStart() {
        return start;
    }

    public void setStart(int start) {
        this.start = start;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public List<SubjectsBean> getSubjects() {
        return subjects;
    }

    public void setSubjects(List<SubjectsBean> subjects) {
        this.subjects = subjects;
    }

    public static class SubjectsBean {
        /**
         * rating : {"max":10,"average":4.9,"stars":"25","min":0}
         * genres : ["喜劇"]
         * title : 妖鈴鈴
         * casts : [{"alt":"https://movie.douban.com/celebrity/1127819/","avatars":{"small":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p45539.webp","large":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p45539.webp","medium":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p45539.webp"},"name":"吳君如","id":"1127819"},{"alt":"https://movie.douban.com/celebrity/1325700/","avatars":{"small":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1356510694.28.webp","large":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1356510694.28.webp","medium":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1356510694.28.webp"},"name":"沈騰","id":"1325700"},{"alt":"https://movie.douban.com/celebrity/1317663/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p40756.webp","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p40756.webp","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p40756.webp"},"name":"岳雲鵬","id":"1317663"}]
         * collect_count : 10595
         * original_title : 妖鈴鈴
         * subtype : movie
         * directors : [{"alt":"https://movie.douban.com/celebrity/1127819/","avatars":{"small":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p45539.webp","large":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p45539.webp","medium":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p45539.webp"},"name":"吳君如","id":"1127819"}]
         * year : 2017
         * images : {"small":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2506825662.webp","large":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2506825662.webp","medium":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2506825662.webp"}
         * alt : https://movie.douban.com/subject/26966580/
         * id : 26966580
         */

        private RatingBean rating;
        private String title;
        private int collect_count;
        private String original_title;
        private String subtype;
        private String year;
        private ImagesBean images;
        private String alt;
        private String id;
        private List<String> genres;
        private List<CastsBean> casts;
        private List<DirectorsBean> directors;

        public RatingBean getRating() {
            return rating;
        }

        public void setRating(RatingBean rating) {
            this.rating = rating;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public int getCollect_count() {
            return collect_count;
        }

        public void setCollect_count(int collect_count) {
            this.collect_count = collect_count;
        }

        public String getOriginal_title() {
            return original_title;
        }

        public void setOriginal_title(String original_title) {
            this.original_title = original_title;
        }

        public String getSubtype() {
            return subtype;
        }

        public void setSubtype(String subtype) {
            this.subtype = subtype;
        }

        public String getYear() {
            return year;
        }

        public void setYear(String year) {
            this.year = year;
        }

        public ImagesBean getImages() {
            return images;
        }

        public void setImages(ImagesBean images) {
            this.images = images;
        }

        public String getAlt() {
            return alt;
        }

        public void setAlt(String alt) {
            this.alt = alt;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public List<String> getGenres() {
            return genres;
        }

        public void setGenres(List<String> genres) {
            this.genres = genres;
        }

        public List<CastsBean> getCasts() {
            return casts;
        }

        public void setCasts(List<CastsBean> casts) {
            this.casts = casts;
        }

        public List<DirectorsBean> getDirectors() {
            return directors;
        }

        public void setDirectors(List<DirectorsBean> directors) {
            this.directors = directors;
        }

        public static class RatingBean {
            /**
             * max : 10
             * average : 4.9
             * stars : 25
             * min : 0
             */

            private int max;
            private double average;
            private String stars;
            private int min;

            public int getMax() {
                return max;
            }

            public void setMax(int max) {
                this.max = max;
            }

            public double getAverage() {
                return average;
            }

            public void setAverage(double average) {
                this.average = average;
            }

            public String getStars() {
                return stars;
            }

            public void setStars(String stars) {
                this.stars = stars;
            }

            public int getMin() {
                return min;
            }

            public void setMin(int min) {
                this.min = min;
            }
        }

        public static class ImagesBean {
            /**
             * small : https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2506825662.webp
             * large : https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2506825662.webp
             * medium : https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2506825662.webp
             */

            private String small;
            private String large;
            private String medium;

            public String getSmall() {
                return small;
            }

            public void setSmall(String small) {
                this.small = small;
            }

            public String getLarge() {
                return large;
            }

            public void setLarge(String large) {
                this.large = large;
            }

            public String getMedium() {
                return medium;
            }

            public void setMedium(String medium) {
                this.medium = medium;
            }
        }

        public static class CastsBean {
            /**
             * alt : https://movie.douban.com/celebrity/1127819/
             * avatars : {"small":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p45539.webp","large":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p45539.webp","medium":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p45539.webp"}
             * name : 吳君如
             * id : 1127819
             */

            private String alt;
            private AvatarsBean avatars;
            private String name;
            private String id;

            public String getAlt() {
                return alt;
            }

            public void setAlt(String alt) {
                this.alt = alt;
            }

            public AvatarsBean getAvatars() {
                return avatars;
            }

            public void setAvatars(AvatarsBean avatars) {
                this.avatars = avatars;
            }

            public String getName() {
                return name;
            }

            public void setName(String name) {
                this.name = name;
            }

            public String getId() {
                return id;
            }

            public void setId(String id) {
                this.id = id;
            }

            public static class AvatarsBean {
                /**
                 * small : https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p45539.webp
                 * large : https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p45539.webp
                 * medium : https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p45539.webp
                 */

                private String small;
                private String large;
                private String medium;

                public String getSmall() {
                    return small;
                }

                public void setSmall(String small) {
                    this.small = small;
                }

                public String getLarge() {
                    return large;
                }

                public void setLarge(String large) {
                    this.large = large;
                }

                public String getMedium() {
                    return medium;
                }

                public void setMedium(String medium) {
                    this.medium = medium;
                }
            }
        }

        public static class DirectorsBean {
            /**
             * alt : https://movie.douban.com/celebrity/1127819/
             * avatars : {"small":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p45539.webp","large":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p45539.webp","medium":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p45539.webp"}
             * name : 吳君如
             * id : 1127819
             */

            private String alt;
            private AvatarsBeanX avatars;
            private String name;
            private String id;

            public String getAlt() {
                return alt;
            }

            public void setAlt(String alt) {
                this.alt = alt;
            }

            public AvatarsBeanX getAvatars() {
                return avatars;
            }

            public void setAvatars(AvatarsBeanX avatars) {
                this.avatars = avatars;
            }

            public String getName() {
                return name;
            }

            public void setName(String name) {
                this.name = name;
            }

            public String getId() {
                return id;
            }

            public void setId(String id) {
                this.id = id;
            }

            public static class AvatarsBeanX {
                /**
                 * small : https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p45539.webp
                 * large : https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p45539.webp
                 * medium : https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p45539.webp
                 */

                private String small;
                private String large;
                private String medium;

                public String getSmall() {
                    return small;
                }

                public void setSmall(String small) {
                    this.small = small;
                }

                public String getLarge() {
                    return large;
                }

                public void setLarge(String large) {
                    this.large = large;
                }

                public String getMedium() {
                    return medium;
                }

                public void setMedium(String medium) {
                    this.medium = medium;
                }
            }
        }
    }
}


使用的是豆瓣電影API,直接用Gsonformat,內容比較多
3. 建立描述網絡請求的接口

package com.paulniu.rxjavademo;

import io.reactivex.Observable;
import retrofit2.http.GET;

/**
 * Created by niupule on 2017/12/30.
 */

public interface GerRequest_Interface {

    @GET("v2/movie/in_theaters")
    Observable<MovieBean> getCall();
    // 註解裏傳入 網絡請求 的部分URL地址
    // Retrofit把網絡請求的URL分紅了兩部分:一部分放在Retrofit對象裏,另外一部分放在網絡請求接口裏
    // 若是接口裏的url是一個完整的網址,那麼放在Retrofit對象裏的URL能夠忽略
    // 採用Observable<...>接口
    // getCall()是接受網絡請求數據的方法

}


    在Activity中實現

package com.paulniu.rxjavademo;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.ListView;

import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.Logger;


import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by niupule on 2017/12/30.
 */

public class RxJava_Douban extends Activity {

    private ListView listview;

    public static final String TAG = "RxJava_Douban";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rxjava_douban);
        listview = findViewById(R.id.listview);
        Logger.addLogAdapter(new AndroidLogAdapter());
        getData();
    }

    private void getData() {
        /**
         * 經過RxJava發送網絡請求
         */
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://api.douban.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();

        GerRequest_Interface request = retrofit.create(GerRequest_Interface.class);
        Observable<MovieBean> call = request.getCall();
        call.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<MovieBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(MovieBean value) {
                        //獲取服務器返回數據
                        Logger.d("hello");
                        Logger.d(value.getSubjects().size());
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}


暫時先寫這麼多,後面關於別的內容,之後再補充

代碼實例
參考資料

輕易&易懂的RxJava入門
RxJava基礎操做符-建立操做符
給初學者的RxJava教程
功能強大的Logger
[RxJava中文文檔]
(https://mcxiaoke.gitbooks.io/rxdocs/content/)
相關文章
相關標籤/搜索