我正在尝试编写一个改进的帮助程序类,所以在这样做时我需要尽可能保持通用。
我正在编写一个创建API调用的方法,它应该返回一个类的列表。因为它是一个辅助类,所以类的类是在运行时确定的,我需要创建一个接收Generic类的方法。 在尝试这样做时,我使用了代码:
客户端界面:
public interface Client{
@GET("{endpoint}")
Call<List<Class<T>>> createGetRequestReturnTypeList(@Path("endpoint") String endPoint);
}
帮助方法:
public void createGetRequest(Class<T> tClass, String endPoint) {
mClient = new Retrofit.Builder().baseUrl("baseUrl").addConverterFactory(GsonConverterFactory.create()).build().create(Client.class);
Call<List<tClass>> call = mClient.createGetRequestReturnType(endPoint);
call.enqueue(new Callback<List<tClass>>() {
@Override
public void onResponse(Call<List<GithubRepo>> call, Response<List<GithubRepo>> response) {
if (response.isSuccessful()) {
List<tClass> class= response.body();
} else {
// The sever error code
switch (response.code()) {
}
}
}
@Override
public void onFailure(Call<List<GithubRepo>> call, Throwable t) {
}
});
}
编写以下方法时,Class<T>
中的T无法解析。
有人可以向我解释为什么,以及如何能够将泛型类作为参数传递?
谢谢