Tolcol 2017-02-04 07:13 采纳率: 50%
浏览 5826
已结题

Android 使用RxJava请求重复

我在点击按钮的时候请求服务器;
但是第二次请求的时候会自动发送两边请求,第三次会发送三次请求;
但是理论上在onCompleted()调用的时候会自动取消订阅,为什么还会这么多请求呢?
求大神帮忙看一下:

点击事件:

 findViewById(R.id.send_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendTohttp();
            }
        });

订阅事件

     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();
            }
        });

    }

网络请求部分

     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(),但是还是一样的效果,求解答

  • 写回答

1条回答 默认 最新

  • 广州-张瑞宝 2017-02-21 01:48
    关注

    1:
    图片说明
    楼主:你的onCompleted()是空的方法,调用执行的是空的方法

    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序