(二)關於RxJava:實例詳解

1 關於實例Hello World 詳解ide

方式一 :spa

  1.1 建立一個Observable對象,直接調用Observable.create便可對象

        Observable<String> myObservable=Observable.create(字符串

            new Observable.onSubscribe<String>(){io

              @Overridecall

               public void call(Subscriber<? super String> sub){co

                   sub.onNext("hello world");字符

                   sub.onCompleted();let

                   }new

            }

        );

    1.2 建立一個Subscriber對象,來處理Observable對象發出的字符串

     Subscriber<String> mySubscriber=new Subscriber<String>()

     {

        @Override

         public void onNext(String s){ System.out.println(s); }

        @Override

         public void onCompleted(){ }

        @Override

         public void onError(Throwable  e){}

     }

    1.3 將兩個對象關聯起來

     myObservable.subscribe(mySubscriber);

方式二:

       2.1建立observable對象

      Observable<String> myObservable=Observable.just("Hello World");

      2.2建立Subscribe對象

      Action1<String> onNextAction=new Action1<String>(){

           @Override

             public void call(String s){

                System.out.println(s); 

              }

        };

   2.3 關聯兩個對象

      myObservable.subscribe(onNextAction);

方式三:

      Observable.just("Hello world")

                       .subscribe(new Action1<String>(){

                        @Override

                         public void call(String  s){

                             System.out.println(s);

                             }

                        });

方式四:

     Observable.just("Hello world") . subscribe(s- > System.out.println(s));

相關文章
相關標籤/搜索