问题
I have an API the returns a standard reply for all requests that gets parsed by gson/retrofit.
public class ServerReply<T> {
@Expose
private String status;
@Expose
private T data;
@Expose
private String message;
}
I have an interface for Retrofit that will return a list of users inside of serverReply.
public interface Test {
@POST("/Test")
void runTest(@Body Body body, Callback<ServerReply<List<User>>> response);
}
I would like to get a different list of objects depending on the content of the body. Is it possible to use templating/generics to accomplish this?(see below)
public interface Test<T> {
@POST("/Test")
void runTest(@Body Body body, Callback<ServerReply<List<T>>> response);
}
回答1:
No, but it's a Java limitation not a missing Retrofit feature. Due to type erasure there is no way for Retrofit to resolve what the type variable T actually is to pass to the deserializer without a concrete class.
来源:https://stackoverflow.com/questions/30929575/does-retrofit-interfaces-support-templated-callbacks