1 关于实例Hello World 详解
方式一 :
1.1 创建一个Observable对象,直接调用Observable.create即可
Observable<String> myObservable=Observable.create(
new Observable.onSubscribe<String>(){
@Override
public void call(Subscriber<? super String> sub){
sub.onNext("hello world");
sub.onCompleted();
}
}
);
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));