我尝试搜索,但没有发现这个确切的问题。我正在尝试创建一个实现RxJava的Observer的抽象类型。这种新类型RemoteObserver将包装Retrofit2 Response对象,该对象包装一些通用类型。我的代码在下面,注释中有错误。
我需要类型A,因为此泛型是由response.body()返回并在onSuccess(..)中使用的类型。
public abstract class RemoteObserver<A, T extends Response<A>> implements Observer<T<A>> { // Error: Type 'T' does not have Type Parameters
@Override
public final void onNext(@NonNull Response response) {
switch (response.code()) {
case HttpsURLConnection.HTTP_OK:
case HttpsURLConnection.HTTP_CREATED:
case HttpsURLConnection.HTTP_ACCEPTED:
case HttpsURLConnection.HTTP_NOT_AUTHORITATIVE:
if (response.body() != null) {
onSuccess(response.body()); // Error: onSuccess(A) cannot be applied to (java.lang.Object)
}
break;
case HttpsURLConnection.HTTP_UNAUTHORIZED:
onUnauthorized();
break;
default:
onError(new Throwable("Default " + response.code() + " " + response.message()));
}
}
public abstract void onSuccess(A response);
public abstract void onUnauthorized();
public abstract void onError(Throwable T);
}
任何帮助将不胜感激。