1、创建被观察者Observable
Observable ob= Observable.create(new Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> subscriber) {
//subscriber主要用来发射数据
subscriber.onNext("sss");
subscriber.onCompleted();
}
});
2、创建观察者
Observer<String> observer=new Observer<String>() {
@Override
public void onCompleted() {
Log.d(TAG, "onCompleted ");
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(String sss) {
Log.d(TAG, "onNext: "+sss);
}
};
3、被观察者订阅观察者
ob.map(new Func1<String,String>() {
@Override
public String call(String o) {
return o+"nihao";
}
}).subscribe(observer);
}
- Fun1接口
/**
* Represents a function with one argument.
* @param <T> the first argument type
* @param <R> the result type 返回参数类型
*/
public interface Func1<T, R> extends Function {
R call(T t);
}
网友评论