Rxjava功能操作符的使用方法詳解
Rxjava功能個(gè)人感覺很好用,里面的一些操作符很方便,Rxjava有:被觀察者,觀察者,訂閱者,
被觀察者通過訂閱者訂閱觀察者,從而實(shí)現(xiàn)觀察者監(jiān)聽被觀察者返回的數(shù)據(jù)
下面把Rxjava常用的模型代碼列出來,還有一些操作符的運(yùn)用:
依賴:
compile 'io.reactivex.rxjava2:rxandroid:2.0.1' // Because RxAndroid releases are few and far between, it is recommended you also // explicitly depend on RxJava's latest version for bug fixes and new features. compile 'io.reactivex.rxjava2:rxjava:2.1.5'
這個(gè)是另一種解析數(shù)據(jù)的方法,阿里巴巴旗下的,聽說是解析最快的解析器。。。。
compile 'com.alibaba:fastjson:1.2.39'
import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.TextView; import com.alibaba.fastjson.JSONObject; import java.io.IOException; import java.util.concurrent.TimeUnit; import io.reactivex.BackpressureStrategy; import io.reactivex.Flowable; import io.reactivex.FlowableEmitter; import io.reactivex.FlowableOnSubscribe; import io.reactivex.Observable; import io.reactivex.ObservableEmitter; import io.reactivex.ObservableOnSubscribe; import io.reactivex.Observer; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.annotations.NonNull; import io.reactivex.disposables.Disposable; import io.reactivex.functions.BiFunction; import io.reactivex.functions.Consumer; import io.reactivex.functions.Function; import io.reactivex.schedulers.Schedulers; import okhttp3.Call; import okhttp3.Callback; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; public class MainActivity extends AppCompatActivity { private TextView name; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); name = (TextView) findViewById(R.id.name); //用來調(diào)用下面的方法,監(jiān)聽。 name.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { interval(); } }); } //例1:Observer public void observer() { //觀察者 Observer<string> observer = new Observer<string>() { @Override public void onSubscribe(@NonNull Disposable d) { } @Override public void onNext(@NonNull String s) { //接收從被觀察者中返回的數(shù)據(jù) System.out.println("onNext :" + s); } @Override public void onError(@NonNull Throwable e) { } @Override public void onComplete() { } }; //被觀察者 Observable<string> observable = new Observable<string>() { @Override protected void subscribeActual(Observer<!--? super String--> observer) { observer.onNext("11111"); observer.onNext("22222"); observer.onComplete(); } }; //產(chǎn)生了訂閱 observable.subscribe(observer); } //例2:Flowable private void flowable(){ //被觀察者 Flowable.create(new FlowableOnSubscribe<string>() { @Override public void subscribe(@NonNull FlowableEmitter<string> e) throws Exception { for (int i = 0; i < 100; i++) { e.onNext(i+""); } } //背壓的策略,buffer緩沖區(qū) 觀察者 //背壓一共給了五種策略 // BUFFER、 // DROP、打印前128個(gè),后面的刪除 // ERROR、 // LATEST、打印前128個(gè)和最后一個(gè),其余刪除 // MISSING //這里的策略若不是BUFFER 那么,會(huì)出現(xiàn)著名的:MissingBackpressureException錯(cuò)誤 }, BackpressureStrategy.BUFFER).subscribe(new Consumer<string>() { @Override public void accept(String s) throws Exception { System.out.println("subscribe accept"+s); Thread.sleep(1000); } }); } //例3:線程調(diào)度器 Scheduler public void flowable1(){ Flowable.create(new FlowableOnSubscribe<string>() { @Override public void subscribe(@NonNull FlowableEmitter<string> e) throws Exception { for (int i = 0; i < 100; i++) { //輸出在哪個(gè)線程 System.out.println("subscribe Thread.currentThread.getName = " + Thread.currentThread().getName()); e.onNext(i+""); } } },BackpressureStrategy.BUFFER) //被觀察者一般放在子線程 .subscribeOn(Schedulers.io()) //觀察者一般放在主線程 .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Consumer<string>() { @Override public void accept(String s) throws Exception { System.out.println("s"+ s); Thread.sleep(100); //輸出在哪個(gè)線程 System.out.println("subscribe Thread.currentThread.getName = " + Thread.currentThread().getName()); } }); } //例4:http請求網(wǎng)絡(luò),map轉(zhuǎn)化器,fastjson解析器 public void map1(){ Observable.create(new ObservableOnSubscribe<string>() { @Override public void subscribe(@NonNull final ObservableEmitter<string> e) throws Exception { OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url("https://qhb.2dyt.com/Bwei/login") .build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { String result = response.body().string(); e.onNext(result); } }); } }) //map轉(zhuǎn)換器 flatmap(無序),concatmap(有序) .map(new Function<string, bean="">() { @Override public Bean apply(@NonNull String s) throws Exception { //用fastjson來解析數(shù)據(jù) return JSONObject.parseObject(s,Bean.class); } }).subscribe(new Consumer<bean>() { @Override public void accept(Bean bean) throws Exception { System.out.println("bean = "+ bean.toString() ); } }); } //常見rxjava操作符 //例 定時(shí)發(fā)送消息 public void interval(){ Observable.interval(2,1, TimeUnit.SECONDS) .take(10) .subscribe(new Consumer<long>() { @Override public void accept(Long aLong) throws Exception { System.out.println("aLong = " + aLong); } }); } //例 zip字符串合并 public void zip(){ Observable observable1 = Observable.create(new ObservableOnSubscribe<string>() { @Override public void subscribe(@NonNull ObservableEmitter<string> e) throws Exception { e.onNext("1"); e.onNext("2"); e.onNext("3"); e.onNext("4"); e.onComplete(); } }); Observable observable2 = Observable.create(new ObservableOnSubscribe<string>() { @Override public void subscribe(@NonNull ObservableEmitter<string> e) throws Exception { e.onNext("A"); e.onNext("B"); e.onNext("C"); e.onNext("D"); e.onComplete(); } }); Observable.zip(observable1, observable2, new BiFunction<string,string,string>() { @Override public String apply(@NonNull String o, @NonNull String o2) throws Exception { return o + o2; } }).subscribe(new Consumer<string>() { @Override public void accept(String o) throws Exception { System.out.println("o"+ o); } }); }
總結(jié)
以上就是本文關(guān)于Rxjava功能操作符的使用方法詳解的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:Javaweb應(yīng)用使用限流處理大量的并發(fā)請求詳解、分享一個(gè)簡單的java爬蟲框架、Java線程之線程同步synchronized和volatile詳解等,有什么問題可以隨時(shí)留言,小編會(huì)及時(shí)回復(fù)大家的。感謝朋友們對本站的支持!
相關(guān)文章
教你通過B+Tree平衡多叉樹理解InnoDB引擎的聚集和非聚集索引
大家都知道B+Tree是從二叉樹演化而來,在這之前我們來先了解二叉樹、平衡二叉樹、平衡多叉樹,這篇文章主要介紹了通過B+Tree平衡多叉樹理解InnoDB引擎的聚集和非聚集索引,需要的朋友可以參考下2022-01-01@TransactionalEventListener的使用和實(shí)現(xiàn)原理分析
這篇文章主要介紹了@TransactionalEventListener的使用和實(shí)現(xiàn)原理分析,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12Arrays.sort如何實(shí)現(xiàn)降序排序
這篇文章主要介紹了Arrays.sort如何實(shí)現(xiàn)降序排序問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11Java?EE實(shí)現(xiàn)用戶后臺(tái)管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java?EE實(shí)現(xiàn)用戶后臺(tái)管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05java實(shí)時(shí)監(jiān)控文件行尾內(nèi)容的實(shí)現(xiàn)
這篇文章主要介紹了java實(shí)時(shí)監(jiān)控文件行尾內(nèi)容的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02SpringBoot?實(shí)現(xiàn)自定義的?@ConditionalOnXXX?注解示例詳解
這篇文章主要介紹了SpringBoot?實(shí)現(xiàn)自定義的?@ConditionalOnXXX?注解,通過示例代碼介紹了實(shí)現(xiàn)一個(gè)自定義的?@Conditional?派生注解,Conditional?派生注解的類如何注入到?spring?容器,需要的朋友可以參考下2022-08-08Java通過Lambda表達(dá)式實(shí)現(xiàn)簡化代碼
我們在編寫代碼時(shí),常常會(huì)遇到代碼又長又重復(fù)的情況,就像調(diào)用第3方服務(wù)時(shí),每個(gè)方法都差不多,?寫起來啰嗦,?改起來麻煩,?還容易改漏,所以本文就來用Lambda表達(dá)式簡化一下代碼,希望對大家有所幫助2023-05-05Java超詳細(xì)講解WebMvcConfigurer攔截器
這篇文章將用實(shí)例來和大家介紹一下WebMvcConfigurer攔截器。文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Java有一定的幫助,需要的可以參考一下2022-06-06