不兼容类型 推断的类型不符合相等约束
所以我有一个模型。Model
public class Model { .... }
这有两个子类:
public class SubmodelA extend Model { .... }
和
public class SubmodelB extend Model { .... }
这三个被包装在类下。Data
public class ApiData<T extends Model> {
public T data;
}
我的将军看起来像这样:response wrapper
public class ApiResponse<DATA> {
DATA data;
}
“虚拟”api 操作保持不变:
public interface Endpoints {
Call<ApiResponse<ApiData>> getData();
}
我有一个实现来处理响应:retrofit2.Callback
public class ApiCallbackProxy<T> implements retrofit2.Callback<T> {
public interface ApiResultListener<RESPONSE_TYPE> {
void onResult(RESPONSE_TYPE response, ApiError error);
}
private ApiResultListener<T> mListener;
private ApiCallbackProxy(ApiResultListener<T> listener) {
mListener = listener;
}
@Override
public void onResponse(Call<T> call, Response<T> response) {
}
@Override
public void onFailure(Call<T> call, Throwable t) {
}
public static <T> ApiCallbackProxy<T> with(ApiResultListener<T> callback) {
return new ApiCallbackProxy<>(callback);
}
}
The ApiClient
public class ApiClient {
public Endpoints mRetrofit;
public ApiClient() {
Retrofit retrofit = new Retrofit.Builder().build();
mRetrofit = retrofit.create(Endpoints.class);
}
public <U extends Model> void getData(ApiResultListener<ApiResponse<ApiData<U>>> callback) {
//Compiler hits here
mRetrofit.getData().enqueue(ApiCallbackProxy.with(callback));
}
}
编译器命中并显示此错误:ApiCallbackProxy.with(callback)
因此,我希望根据此 API 调用在应用中的位置返回模型的不同子类或模型本身。
即。
public static void main (String[] args) {
ApiClient apiClient = new ApiClient();
apiClient.getData(listener2);
}
public static final ApiResultListener<ApiResponse<Data<SubmodelA>>> listener = (response, error) -> {};
public static final ApiResultListener<ApiResponse<Data<Model>>> listener2 = (response, error) -> {};
public static final ApiResultListener<ApiResponse<Data<SubmodelB>>> listener3 = (response, error) -> {};