简体   繁体   English

如何在 Retrofit 中使用泛型类型 T?

[英]How do I use the generic type T with Retrofit?

I want to use T in Retrofit, but I do not know, how to set T in Retrofit.create(X.class) method.我想在 Retrofit 中使用 T,但我不知道如何在 Retrofit.create(X.class) 方法中设置 T。

public interface SupportService<T> {
    @GET
    Flowable<BaseResponse<T>> getApi(@Url String url, @Query("data") JsonObject jsonObject);
}

HttpRetrofit.getInstance()
                .getRetrofit(this)
                .<SupportService<T>>create(SupportService.class)
                .getApi("",null)
public class BaseResponse<T>{
    public int code;
    public String message;
    public T data;   
}

"T" stays for a generic type of your choice. “T”代表您选择的通用类型。 In retrofit, you can define your data model class accordingly to api response, for example with @SerializedName annotation if you are using GSON as serializing library.在改造中,您可以相应地定义你的数据模型类API响应,例如与@SerializedName如果您正在使用GSON作为序列化库的注释。 That "T" type will be the class defined for the api response. “T”类型将是为 api 响应定义的类。 Here you can find a simple starting point tutorial about retrofit. 在这里,您可以找到有关改造的简单入门教程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM