RxJava

来源:互联网 发布:沸点滑板怎么样知乎 编辑:程序博客网 时间:2024/06/02 01:59

RxJava 简单实用

来源:http://blog.csdn.net/qq_35064774/article/details/53057332

1、app build.gradle中添加

dependencies {    compile 'io.reactivex.rxjava2:rxjava:2.0.0'    compile 'org.reactivestreams:reactive-streams:1.0.0'}

2、测试代码

package com.test.main;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.util.Log;import org.reactivestreams.Subscriber;import org.reactivestreams.Subscription;import io.reactivex.BackpressureStrategy;import io.reactivex.Flowable;import io.reactivex.FlowableEmitter;import io.reactivex.FlowableOnSubscribe;import io.reactivex.functions.Consumer;import io.reactivex.functions.Function;/** * http://blog.csdn.net/qq_35064774/article/details/53057332 * RxJava 察者模式+迭代器模式+函数式编程 */public class MainActivity extends AppCompatActivity {    private static final String TAG = "MainActivity222222";    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);//        testOne();//        testTwo();//        test3();//        test4();    }    private void testOne() {        //创建一个Flowable对象很简单,直接调用Flowable.create即可。        Flowable<String> flowable = Flowable.create(new FlowableOnSubscribe<String>() {            @Override            public void subscribe(FlowableEmitter<String> e) throws Exception {                Log.i(TAG, "FlowableOnSubscribe subscribe: ");                e.onNext("Hello Rxjava2");//发射了一个字符串"hello RxJava 2"。                e.onComplete();            }        }, BackpressureStrategy.BUFFER);//        创建一个Subscriber        Subscriber subscriber = new Subscriber<String>() {            @Override            public void onSubscribe(Subscription s) {                Log.i(TAG, "onSubscribe: ");                s.request(Long.MAX_VALUE);//调用request去请求资源,参数就是要请求的数            }            @Override            public void onNext(String s) {                Log.i(TAG, "onNext: s= " + s);//onNext方法里面传入的参数就是Flowable中发射出来的。            }            @Override            public void onError(Throwable t) {            }            @Override            public void onComplete() {                Log.i(TAG, "onComplete: ");            }        };        flowable.subscribe(subscriber);//为了让”发射器”和”接收器”工作起来,我们还需要把他们组装在一起。    }    private void testTwo() {        Flowable<String> flowable = Flowable.just("hello Rxjava 2");//直接调用Flowable.just创建一个发射字符串的”发射器”。        Consumer consumer = new Consumer<String>() {//对于 Subscriber 来说,我们目前仅仅关心onNext方法            @Override            public void accept(String s) throws Exception {                Log.i(TAG, "accept: s =" + s);            }        };        flowable.subscribe(consumer);    }    private void test3() {//操作符        Flowable.just("map")                .map(new Function<String, String>() {                    @Override                    public String apply(String s) throws Exception {                        Log.i(TAG, "apply: s = " + s);                        return s + "-day day up";// map 是把传递过来的结果末尾加上了签名,然后在传递给了订阅者。                    }                })                .subscribe(new Consumer<String>() {                    @Override                    public void accept(String s) throws Exception {                        Log.i(TAG, "accept: s = " + s);                    }                });    }    private void test4() {//map 操作符进阶        Flowable.just("map1")                .map(new Function<String, Integer>() {                    @Override                    public Integer apply(String s) throws Exception {                        Log.i(TAG, "apply:  s.hashCode() = " + s.hashCode());                        return s.hashCode();                    }                })                .map(new Function<Integer, String>() {                    @Override                    public String apply(Integer integer) throws Exception {                        Log.i(TAG, "apply: integer = " + integer);                        return integer.toString();                    }                }).subscribe(new Consumer<String>() {            @Override            public void accept(String s) throws Exception {                Log.i(TAG, "accept: s = " + s);            }        });    }    private void test5() {//map 创建操作符//        Observable.timer(3, TimeUnit.SECONDS, AndroidSchedulers.mainThread()).map(new Function<Long, Object>() {//        })//        Flowable.just("map1")//                .map(new Function<String, Integer>() {//                    @Override//                    public Integer apply(String s) throws Exception {//                        Log.i(TAG, "apply:  s.hashCode() = " + s.hashCode());//                        return s.hashCode();//                    }//                })//                .map(new Function<Integer, String>() {//                    @Override//                    public String apply(Integer integer) throws Exception {//                        Log.i(TAG, "apply: integer = " + integer);//                        return integer.toString();//                    }//                }).subscribe(new Consumer<String>() {//            @Override//            public void accept(String s) throws Exception {//                Log.i(TAG, "accept: s = " + s);//            }//        });    }}


0 1
原创粉丝点击