RxJava1.6订阅和取消订阅

/**
 * @author ChenYe
 *         implementation 'io.reactivex:rxjava:1.1.6'
 *         implementation 'io.reactivex:rxandroid:1.2.1'
 *         RxJava的观察者和被观察者,当观察者的代码块完全走完之后(请看清楚,我说的完全,不是说执行一次之后),
 *         就会自动取消订阅,然后观察者的一个方法subscriber.isUnsubscribed(),返回true代表已经取消订阅了,
 *         返回false代表还在订阅中,而这个时候执行 subscriber.unsubscribe();就可以取消订阅
 */
public class MainActivity extends AppCompatActivity {

    private static final String TAG = "cy==MainActivity";
    private Observable<String> observable;
    private Subscriber<String> subscriber;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        observable = Observable.interval(10, TimeUnit.SECONDS)
                .map(new Func1<Long, String>() {
                    @Override
                    public String call(Long aLong) {
                        return "这是测试数据";
                    }
                });
        subscriber = Subscribers.create(new Action1<String>() {
            @Override
            public void call(String s) {
                Log.e(TAG, s);
            }
        });

    }


    public void subAndExecute(View view) {
        observable.subscribe(subscriber);
    }

    public void cancelSub(View view) {

        if (!subscriber.isUnsubscribed()) {
            subscriber.unsubscribe();
        }
    }

    public void judge(View view) {
        //指示此订阅者是否已从其订阅列表取消订阅。
        if (subscriber.isUnsubscribed()) {
            Log.e(TAG, "已经取消了订阅");
        } else {
            Log.e(TAG, "还没有取消订阅");
        }
    }

}

发布了139 篇原创文章 · 获赞 22 · 访问量 10万+
展开阅读全文

Android 使用RxJava请求重复

02-04

我在点击按钮的时候请求服务器; 但是第二次请求的时候会自动发送两边请求,第三次会发送三次请求; 但是理论上在onCompleted()调用的时候会自动取消订阅,为什么还会这么多请求呢? 求大神帮忙看一下: 点击事件: ``` java findViewById(R.id.send_button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { sendTohttp(); } }); ``` 订阅事件 ```java private void sendTohttp() { HttpManager.getInstance().rxHttp("http://hehero.com/test/tb.html").subscribe(new Subscriber<String>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(String s) { mTextView.setText(s); onCompleted(); } }); } ``` 网络请求部分 ```java public Observable<String> rxHttp(final String url) { return Observable.create(new Observable.OnSubscribe<String>() { @Override public void call(Subscriber<? super String> subscriber) { try { //OkHttp请求 Response response = getCall(url).execute(); if (response.isSuccessful()) { subscriber.onNext(response.body().string()); } else { subscriber.onError(new Exception(response.message())); } } catch (IOException e) { subscriber.onError(e); } } }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()); } ``` 日志如下: ``` //第一遍请求 D/OkHttp: --> POST http://hehero.com/test/tb.html http/1.1 (9-byte body) D/OkHttp: <-- 200 OK http://hehero.com/test/tb.html (103ms, unknown-length body) //清除日志第二遍请求 --> POST http://hehero.com/test/tb.html http/1.1 (9-byte body) --> POST http://hehero.com/test/tb.html http/1.1 (9-byte body) <-- 200 OK http://hehero.com/test/tb.html (73ms, unknown-length body) <-- 200 OK http://hehero.com/test/tb.html (73ms, unknown-length body) //清除日志 第三遍请求 --> POST http://hehero.com/test/tb.html http/1.1 (9-byte body) --> POST http://hehero.com/test/tb.html http/1.1 (9-byte body) --> POST http://hehero.com/test/tb.html http/1.1 (9-byte body) <-- 200 OK http://hehero.com/test/tb.html (71ms, unknown-length body) <-- 200 OK http://hehero.com/test/tb.html (71ms, unknown-length body) <-- 200 OK http://hehero.com/test/tb.html (72ms, unknown-length body) ``` 问题:就是每次请求好像以前的订阅关系并没有取消订阅,而是重新成生一个新的订阅,我尝试过主动unsubscribe(),但是还是一样的效果,求解答 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览