Does retrofit interfaces support templated callbacks

核能气质少年 提交于 2019-12-13 08:01:31

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!