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 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?