I'm using Retrofit2 in my app and it works really fine, but i am trying to use a single interface for all kind of GET method requests by passing a dynamic model. When i try it using following code, it gives error at runtime
public interface LoadDataServiceTest<T> {
@GET
Call<T> getModel(@Url String url, @QueryMap Map<String, String> options);
}
Model:
public class ModelTest<T> {
@SerializedName("status")
private String status;
@SerializedName("message")
private String message;
@SerializedName("users")
private T data;
public String getStatus() {
return status;
}
public String getMessage() {
return message;
}
public T getData() {
return data;
}
}
But when i create service as following, then i get error. How can i resolve it and what is the best approach to achieve this goal.
LoadDataServiceTest<ModelTest<JsonArray>> service = retrofit.create((Class<LoadDataServiceTest<ModelTest<JsonArray>>>) (Class<?>) APIs.LoadDataServiceTest.class);
Map<String, String> parameters = new HashMap<>();
parameters.put("user_id",userId);
Call<ModelTest<JsonArray>> call = service.getModel(APIs.GET_USERS, parameters);
Error:
FATAL EXCEPTION: main Process: com.iu.colgatepalmolive, PID: 16808 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.iu.colgatepalmolive/com.iu.hfl_ccp.LoginActivity}: java.lang.IllegalArgumentException: Method return type must not include a type variable or wildcard: retrofit2.Call for method LoadDataServiceTest.getModel at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2666) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2727) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1478) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6121) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779) Caused by: java.lang.IllegalArgumentException: Method return type must not include a type variable or wildcard: retrofit2.Call for method LoadDataServiceTest.getModel at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:720) at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:711) at retrofit2.ServiceMethod$Builder.createCallAdapter(ServiceMethod.java:224) at retrofit2.ServiceMethod$Builder.build(ServiceMethod.java:160) at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:166) at retrofit2.Retrofit$1.invoke(Retrofit.java:145) at java.lang.reflect.Proxy.invoke(Proxy.java:813) at $Proxy5.getModel(Unknown Source)