RxJava的简单使用(一)

一测试订阅

@Test
public void testSubscribe() {
    //观察者/订阅者
    final Subscriber<String> subscriber =
            new Subscriber<String>() {
                @Override
                public void onCompleted() {
                    System.out.println("onCompleted in tread:" +
                            Thread.currentThread().getName());

                }

                @Override
                public void onError(Throwable e) {
                    System.out.println("onError in tread:" +
                            Thread.currentThread().getName());
                    e.printStackTrace();
                }

                @Override
                public void onNext(String s) {
                    System.out.println("onNext in tread:" +
                            Thread.currentThread().getName());
                    System.out.println(s);
                }
            };

    //被观察者
    Observable observable = Observable.create(
            new Observable.OnSubscribe<Subscriber>() {
                @Override
                public void call(Subscriber subscriber1) {
                    // 发生事件
                    System.out.println("call in tread:" + Thread.currentThread().getName());
                    subscriber1.onStart();
                    subscriber1.onError(new Exception("error"));
                    subscriber1.onNext("hello world");
                    subscriber1.onCompleted();
                }
            });

    //订阅
    observable.subscribe(subscriber);

}
二、测试线程

@Test
public void testScheduler() {

    //观察者/订阅者
    final Subscriber<String> subscriber = new Subscriber<String>() {
        @Override
        public void onCompleted() {
            System.out.println("onCompleted in tread:" +
                    Thread.currentThread().getName());
        }

        @Override
        public void onError(Throwable e) {
            System.out.println("onError in tread:" +
                    Thread.currentThread().getName());
            e.printStackTrace();
        }

        @Override
        public void onNext(String s) {
            System.out.println("onNext in tread:" +
                    Thread.currentThread().getName());
            System.out.println(s);
        }
    };

    //被观察者
    Observable observable = Observable.create(
            new Observable.OnSubscribe<Subscriber>() {
                @Override
                public void call(Subscriber subscriber1) {
                    // 发生事件
                    System.out.println("call in tread:" +
                            Thread.currentThread().getName());
                    subscriber1.onStart();
                    subscriber1.onNext("hello world");
                    subscriber1.onCompleted();

                }
            });

    //订阅
    observable.subscribeOn(Schedulers.io())  // 指定生产事件在当前 线程中进行
            .observeOn(Schedulers.newThread()) //  指定消费事件在新线程中进行
            .subscribe(subscriber);


}
三、测试节点

  @Test
    public void testMap() {

        String name = "yijia";
        Observable.just(name)
                .subscribeOn(Schedulers.newThread()) // 指定下一个生成节点在新线程中处理
                .map(new Func1<String, User>() {
                    @Override
                    public User call(String name) {
                        User user = new User();
                        user.setName(name);
                        System.out.println("process User call in tread:"
                                + Thread.currentThread().getName());
                        return user;
                    }
                })

                .subscribeOn(Schedulers.newThread()) // 指定下一个生产节点在新线程中处理
                .map(new Func1<User, Object>() {
                    @Override
                    public Object call(User user) {
                        // 如果需要,我们在这里还可以对 User 进行加工
                        System.out.println("process User call in tread:"
                                + Thread.currentThread().getName());
                        return user;
                    }
                })

                .observeOn(Schedulers.newThread()) // 指定消费节点在新线程中处理
                .subscribe(new Action1<Object>() {
                    @Override
                    public void call(Object data) {

                        System.out.println("receive User call in tread:"
                                + Thread.currentThread().getName());
                    }
                });
    }
    public static class User {
        String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

}

四、rxjava在android中的使用

@RunWith(AndroidJUnit4.class)
public class TestRxJavaInAndroid {
    @Test
    public void testMapInAndroid(){
        Observable.just("yijia")
                .subscribeOn(Schedulers.io())// 指定下一个产生的线程节点在 IO 线程中处理
                .map(new Func1<String, User>() {
                    @Override
                    public User call(String name) {
                        User user = new User();
                        user.setName(name);
                        System.out.println("process User call in tread:" +
                                Thread.currentThread().getName());
                        return user;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<Object>() {
                    @Override
                    public void call(Object s) {
                        System.out.println("receive User call in tread:"
                                + Thread.currentThread().getName());
                    }
                });
                
    }
    public static class User {
        String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值