【发布时间】:2017-11-03 01:46:05
【问题描述】:
所以我有一个模型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);
}
}
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) -> {};
【问题讨论】:
-
什么是
ChallengeData? -
我的意思是
Payload:) 我将实际名称更改为更通用的名称。 -
我明白了,我认为您还有一些类似的错误。如果您可以发布minimal reproducible example,那就太好了
-
@Manos 你可以添加字段
mRetrofitApiInterface的声明代码以及至少包含其签名的封闭类的摘录(名称、类型参数、超类/接口列表...)和关键领域/方法。谢谢。 -
如果您提供minimal reproducible example,回答这个问题会容易得多。我们可以在没有其他依赖项的情况下进行编译,然后查看错误。
标签: java generics retrofit retrofit2