1. 程式人生 > >01_RxJava操作符示例程式碼

01_RxJava操作符示例程式碼

    抽時間研究了下RxJava,因為網路博文講述描述性文字較多,示例較少,很多時候不知道怎麼開始入手學習RxJava,因此在此發文讓想入門的看了博文之後實操一下,步驟很簡單:

    1.在網上下載一個RxJava的jar包,我找到一個1.1.5jar包,也不用管版本的高低,先入門是沒問題的,寫幾個例子一點問題沒有,什麼RxJava2,大家大可不必慌張去學習,研究低版本的,是讓我們知道低層版本是怎樣做的,在發展到RxJava2的時候,又做了哪些改進,所以遵從一個由低到高的順序,你不會吃虧的。(扯遠了)

    2.在Eclipse裡面建一個java工程,新建一個libs目錄,將下載到的RxJava 的jar包放到該目錄下,然後新增到構建路徑(add lib to build path).

    3.接下來就可以建一個包,然後建一個類,開始在main()裡面寫示例程式碼了,學東西先讓它執行起來再看是怎麼回事兒。接下來貼一下我學習時寫的hello world與操作符使用的示例程式碼,供大家參考,希望對入門的人有所幫助。


package com.gdc.rxjava;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.Observable.OnSubscribe;
import rx.functions.Func0;
import rx.Subscriber;

public class HelloWorld {

	public static void main(String[] args) {
		// helloworld();
		// create();
		// just();
		// from();
		// defer();
		// empty();
		// never();
		// throwtest();
		// interval();
		// range();
		repeat();
	}

	private static void create() {
		Observable.create(new OnSubscribe<String>() {

			@Override
			public void call(Subscriber<? super String> subscriber) {
				subscriber.onNext("中華人民共和國 RxJava學習");
			}
		}).subscribe(new Subscriber<String>() {

			@Override
			public void onCompleted() {
				System.out.println("onCompleted()");
			}

			@Override
			public void onError(Throwable arg0) {
				System.out.println("onError():" + arg0);
			}

			@Override
			public void onNext(String arg0) {
				System.out.println("onNext():" + arg0);
			}
		});
	}

	private static void just() {
		Observable.just("中華人民共和國 RxJava學習").subscribe(new Subscriber<String>() {

			@Override
			public void onCompleted() {
				System.out.println("onCompleted()");
			}

			@Override
			public void onError(Throwable arg0) {
				System.out.println("onError():" + arg0);
			}

			@Override
			public void onNext(String arg0) {
				System.out.println("onNext():" + arg0);
			}
		});
	}

	private static void from() {
		// 1: 陣列、
		// 2:列表
		// 3:Iterable
		// 4:Future
		// 5:Future,Scheduler
		// 6:Future,timout,timeUnit
		Integer[] a = new Integer[] { 1, 2, 3, 4, 5, 6, 7 };
		// 支援2
		ArrayList<Integer> items = new ArrayList<Integer>();
		items.add(1);
		items.add(2);
		items.add(3);
		items.add(4);
		items.add(5);
		items.add(6);
		Observable.from(items).subscribe(new Subscriber<Integer>() {

			@Override
			public void onCompleted() {
				System.out.println("onCompleted()");
			}

			@Override
			public void onError(Throwable arg0) {
				System.out.println("onError():" + arg0);
			}

			@Override
			public void onNext(Integer arg0) {
				System.out.println("onNext():" + arg0);
			}
		});
	}

	private static String valuestr;

	private static void defer() {
		// Observable observable = Observable.just(valuestr);

		Observable observable = Observable.defer(new Func0<Observable<String>>() {

			@Override
			public Observable<String> call() {
				return Observable.just(valuestr);
			}
		});

		valuestr = "中華人民共和國RxJava學習";

		observable.subscribe(new Subscriber<String>() {

			@Override
			public void onCompleted() {
				System.out.println("onCompleted()");
			}

			@Override
			public void onError(Throwable arg0) {
				System.out.println("onError():" + arg0);
			}

			@Override
			public void onNext(String arg0) {
				System.out.println("onNext():" + arg0);
			}
		});
	}

	private static void empty() {
		Observable.empty().subscribe(new Subscriber() {

			@Override
			public void onCompleted() {
				System.out.println("onCompleted()");
			}

			@Override
			public void onError(Throwable arg0) {
				System.out.println("onError():" + arg0);
			}

			@Override
			public void onNext(Object arg0) {

				System.out.println("onNext()");
			}
		});
	}

	private static void never() {
		Observable.never().subscribe(new Subscriber() {

			@Override
			public void onCompleted() {
				System.out.println("onCompleted()");
			}

			@Override
			public void onError(Throwable arg0) {
				System.out.println("onError():" + arg0);
			}

			@Override
			public void onNext(Object arg0) {

				System.out.println("onNext()");
			}
		});
	}

	private static void throwtest() {
		Observable.error(new NullPointerException()).subscribe(new Subscriber() {

			@Override
			public void onCompleted() {
				System.out.println("onCompleted()");
			}

			@Override
			public void onError(Throwable arg0) {
				System.out.println("onError():" + arg0);
			}

			@Override
			public void onNext(Object arg0) {

				System.out.println("onNext()");
			}
		});
	}
	
	private static void interval(){
		Observable.interval(1000,TimeUnit.MICROSECONDS).subscribe(new Subscriber() {

			@Override
			public void onCompleted() {
				System.out.println("onCompleted()");
			}

			@Override
			public void onError(Throwable arg0) {
				System.out.println("onError():" + arg0);
			}

			@Override
			public void onNext(Object arg0) {

				System.out.println("onNext()");
			}
		});
	}
	
	private static void range(){
		Observable observable = Observable.range(1, 5);
		observable.subscribe(new Subscriber<Integer>() {

			@Override
			public void onCompleted() {
				System.out.println("onCompleted()");
			}

			@Override
			public void onError(Throwable arg0) {
				System.out.println("onError():" + arg0);
			}

			@Override
			public void onNext(Integer arg0) {
				System.out.println("onNext()" + arg0);
			}
		});
	}
	
	private static void repeat(){
		Observable observable = Observable.range(1, 5).repeat(2);
		observable.subscribe(new Subscriber<Integer>() {

			@Override
			public void onCompleted() {
				System.out.println("onCompleted()");
			}

			@Override
			public void onError(Throwable arg0) {
				System.out.println("onError():" + arg0);
			}

			@Override
			public void onNext(Integer arg0) {
				System.out.println("onNext()" + arg0);
			}
		});
	}
	

	private static void helloworld() {
		// s1.建立被觀察者
		Observable myObservable = Observable.create(new OnSubscribe<String>() {

			@Override
			public void call(Subscriber<? super String> subscriber) {
				subscriber.onNext("hello world!!!");
				// subscriber.onCompleted();
				throw new NullPointerException();
			}
		});

		// s2.建立觀察者
		Subscriber subscriber = new Subscriber<String>() {

			@Override
			public void onCompleted() {
				System.out.println("onCompleted()");
			}

			@Override
			public void onError(Throwable arg0) {
				System.out.println("onError()");
			}

			@Override
			public void onNext(String arg0) {
				System.out.println("onNext():" + arg0);
			}

		};

		// s3.訂閱事件
		myObservable.subscribe(subscriber);
	}

}