我在点击按钮的时候请求服务器;
但是第二次请求的时候会自动发送两边请求,第三次会发送三次请求;
但是理论上在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(),但是还是一样的效果,求解答