问题是我可以使用 anObserver
但我不能使用SingleObserver
. 它给了我这个错误:
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>'
和
.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
以下是一些实现细节:
//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
);
故障在于:
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
}
});
如果我替换SingleObserver
为Observer
( 并实现它编译好的所有必需的onXXX()
方法。但我想要一个观察者。
我应该降级到 rxjava2(未经测试)吗?
我怀疑我的进口产品很混乱。
似乎有 2 个SingleObserver
,另一个在io.reactivex.SingleObserver
.
我确实尝试使用这些导入并跳过了rxjava3
大多数的前缀。不过AndroidSchedulers
只能在io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
.