简体   繁体   中英

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.

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. 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. That "T" type will be the class defined for the api response. Here you can find a simple starting point tutorial about retrofit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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