Create: 创建一个Observable
public static void rxcreate() { Observable.create(new Observable.OnSubscribe<String>() { @Override public void call(Subscriber<? super String> t) { // TODO Auto-generated method stub if(!t.isUnsubscribed()) { for(int i=0;i<5;i++) { t.onNext(i+""); } t.onCompleted(); } } }).subscribe(new Subscriber<String>() { @Override public void onCompleted() { // TODO Auto-generated method stub System.out.println("onCompleted: "); } @Override public void onError(Throwable e) { // TODO Auto-generated method stub } @Override public void onNext(String t) { // TODO Auto-generated method stub System.out.println("Next: " + t); } }); }
Defer: 直到订阅发生时才创建
public static void rxdefer() { Observable.defer(new Func0<Observable<String>>() { @Override public Observable<String> call() { // TODO Auto-generated method stub System.out.println("call"); String[] s={"zhangshan","lisi"}; return Observable.from(s); } }).delaySubscription(1000, TimeUnit.MILLISECONDS) . subscribe(System.out::println); try { Thread.currentThread().sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
Empty:创建一个不发送内容但会正常结束的
NEVER:创建一个不发送内容但不会正常结束的
Error: 创建一个不发送内容但以错误终止的
Observable.empty().subscribe(new Subscriber<Object>() { @Override public void onCompleted() { // TODO Auto-generated method stub System.out.println("onCompleted: "); } @Override public void onError(Throwable e) { // TODO Auto-generated method stub System.out.println("onError: "); } @Override public void onNext(Object t) { // TODO Auto-generated method stub System.out.println("onNext: "+t); } });
Observable.error(new Throwable("error ")).subscribe(new Subscriber<Object>() { @Override public void onCompleted() { // TODO Auto-generated method stub System.out.println("onCompleted: "); } @Override public void onError(Throwable e) { // TODO Auto-generated method stub System.out.println("onError: "); } @Override public void onNext(Object t) { // TODO Auto-generated method stub System.out.println("onNext: "+t); } });
Observable.never().subscribe(new Subscriber<Object>() { @Override public void onCompleted() { // TODO Auto-generated method stub System.out.println("onCompleted: "); } @Override public void onError(Throwable e) { // TODO Auto-generated method stub System.out.println("onError: "); } @Override public void onNext(Object t) { // TODO Auto-generated method stub System.out.println("onNext: "+t); } });
From:通过对象或数据结构创建
array iterate
public static void rxFrom() { String[] s = {"ame","dsklj","dee"}; List<String> list = Arrays.asList(s); Observable.from(list).subscribe(new Subscriber<Object>() { @Override public void onCompleted() { // TODO Auto-generated method stub System.out.println("onCompleted: "); } @Override public void onError(Throwable e) { // TODO Auto-generated method stub System.out.println("onError: "); } @Override public void onNext(Object t) { // TODO Auto-generated method stub System.out.println("onNext: "+t); } }); } View Code
Future
FutureTask<String> ft=new FutureTask<>(cal); Observable.from(ft,Schedulers.io()) .doOnSubscribe(()-> ft.run()) .subscribeOn(Schedulers.io()) .subscribe(new Subscriber<Object>() { @Override public void onCompleted() { // TODO Auto-generated method stub System.out.println("onCompleted: "); } @Override public void onError(Throwable e) { // TODO Auto-generated method stub System.out.println("onError: "); } @Override public void onNext(Object t) { // TODO Auto-generated method stub System.out.println("onNext: "+t); } }); try { Thread.sleep(3000); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); }
Interval: 间隔一段时间发送
} public static void rxInterval() { String[] s = {"ame","dsklj","dee"}; Observable.interval(1000,TimeUnit.MILLISECONDS).take(5).subscribe(new Subscriber<Object>() { @Override public void onCompleted() { // TODO Auto-generated method stub System.out.println("onCompleted: "); } @Override public void onError(Throwable e) { // TODO Auto-generated method stub System.out.println("onError: "); } @Override public void onNext(Object t) { // TODO Auto-generated method stub System.out.println("onNext: "+t); } }); try { Thread.currentThread().sleep(5000); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }
Just: 发布一个对象
public static void rxJust() { Observable.just(1, 2, 3) .subscribe(new Subscriber<Integer>() { @Override public void onNext(Integer item) { System.out.println("Next: " + item); } @Override public void onError(Throwable error) { System.err.println("Error: " + error.getMessage()); } @Override public void onCompleted() { System.out.println("Sequence complete."); } }); }
Range:发布一个整数区域
Observable.range(0, 10) .subscribe(new Subscriber<Integer>() { @Override public void onNext(Integer item) { System.out.println("Next: " + item); } @Override public void onError(Throwable error) { System.err.println("Error: " + error.getMessage()); } @Override public void onCompleted() { System.out.println("Sequence complete."); } });
Repeat: 重复次数
Observable.range(0, 10).repeat(3) .subscribe(new Subscriber<Integer>() { @Override public void onNext(Integer item) { System.out.println("Next: " + item); } @Override public void onError(Throwable error) { System.err.println("Error: " + error.getMessage()); } @Override public void onCompleted() { System.out.println("Sequence complete."); } });
Start:
public static void rxStart() { Observable.fromCallable(new Callable<String>() { @Override public String call() throws Exception { // TODO Auto-generated method stub System.out.println("call me"); return null; } }).subscribe(new Subscriber<Object>() { @Override public void onCompleted() { // TODO Auto-generated method stub System.out.println("onCompleted: "); } @Override public void onError(Throwable e) { // TODO Auto-generated method stub System.out.println("onError: "); } @Override public void onNext(Object t) { // TODO Auto-generated method stub System.out.println("onNext: "+t); } }); try { Thread.sleep(3000); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } View Code
Timer: 延迟一段时间后发布
public static void rxTimer() { long time1=System.currentTimeMillis(); Observable.timer(2000, TimeUnit.MILLISECONDS) .subscribe(new Subscriber<Long>() { @Override public void onNext(Long item) { System.out.println("start ="+(System.currentTimeMillis()-time1)); System.out.println("Next: " + item); } @Override public void onError(Throwable error) { System.err.println("Error: " + error.getMessage()); } @Override public void onCompleted() { System.out.println("Sequence complete."); } }); try { Thread.currentThread().sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }