改装界面是否支持模板化回调 [英] Does retrofit interfaces support templated callbacks

查看:90
本文介绍了改装界面是否支持模板化回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 公共类ServerReply< p>我有一个API,返回所有请求的标准回复,并被gson / retrofit解析。 ; T> {
@Expose
私人字符串状态;
@Expose
私人T数据;
@Expose
私人字符串消息;

$ / code>

我有一个Retrofit的接口,它将返回serverReply中的用户列表。

  public interface Test {
@POST(/ Test)
void runTest(@Body Body body,Callback< ServerReply< List< User>>> response);
}

我想根据内容获取不同的对象列表身体。是否有可能使用模板/泛型来实现这一点?(见下文)

  public interface Test< T> {
@POST(/ Test)
void runTest(@Body Body body,Callback< ServerReply< List< T>>> response);


解决方案

不,但它是一个Java限制不是缺少改造功能。由于类型擦除,Retrofit无法解决类型变量 T 实际上没有具体类传递给解串器的情况。


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);
}

解决方案

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.

这篇关于改装界面是否支持模板化回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆