The problem is that I can use an Observer
but I can't use a SingleObserver
. It gives me this error:
Inferred type 'E' for type parameter 'E' is not within its bound; should implement 'io.reactivex.rxjava3.core.Observer<? super com.xxx.Network.Response.ResponseLogin>'
and
.subscribeWith(new SingleObserver<ResponseLogin>(){
^
required: E
found: <anonymous SingleObserver<ResponseLogin>>
reason: inferred type does not conform to upper bound(s)
inferred: <anonymous SingleObserver<ResponseLogin>>
upper bound(s): Observer<? super ResponseLogin>
where E,T are type-variables:
E extends Observer<? super ResponseLogin> declared in method <E>subscribeWith(E)
T extends Object declared in class Observable
Here are some implementation details:
//build.gradle
implementation "io.reactivex.rxjava3:rxandroid:3.0.0"
implementation "io.reactivex.rxjava3:rxjava:3.0.2"
//ApiInterface.java
import io.reactivex.rxjava3.core.Observable;
...
public interface ServerAPIInterface {
// For POST request
@FormUrlEncoded
@POST("/login")
public Observable<ResponseLogin> login(
// the POST params to server
@Field("user_email") String user_email,
@Field("user_password") String user_password
);
and the fault is at:
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.core.SingleObserver;
import io.reactivex.rxjava3.disposables.CompositeDisposable;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;
...
myApiInterface
.login(myUserEmail, myPassword)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new SingleObserver<ResponseLogin>() {
@Override
public void onSuccess(ResponseLogin data) {
// update the adapter
}
@Override
public void onSubscribe(Disposable d) {
disposables.add(d);
}
@Override
public void onError(Throwable e) {
// display an error message
}
});
If I replace SingleObserver
with Observer
(and implement all the required onXXX()
methods it compiles OK. But I want a single observer.
Shall I downgrade to rxjava2 (untested)?
I suspect my imports are confused.
it seems there are 2 SingleObserver
, another one is at io.reactivex.SingleObserver
.
I did try to use these imports and skipped the rxjava3
prefix for most of them. However AndroidSchedulers
can only be found in io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
.
question from:
https://stackoverflow.com/questions/65908423/rxjava3-on-android-java-problem-using-singleobserver-but-observer-is-fine 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…