导依赖
implementation 'com.squareup.retrofit2:retrofit:2.0.0'
implementation 'com.squareup.retrofit2:converter-gson:2.0.2'
创建类
public class RetrofitUtils {
private static RetrofitUtils retrofitUtils;
private RetrofitUtils(){}
public static RetrofitUtils getInstance(){
if (retrofitUtils==null){
synchronized (RetrofitUtils.class){
if (retrofitUtils==null){
retrofitUtils=new RetrofitUtils();
}
}
}
return retrofitUtils;
}
private static Retrofit retrofit;
public static synchronized Retrofit getRetrofit(String ShowUrl){
retrofit=new Retrofit.Builder()
.baseUrl(ShowUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit;
}
public <T> T getApiService(String ShowUrl,Class<T> service){
Retrofit retrofit=getRetrofit(ShowUrl);
T t=retrofit.create(service);
return t;
}
}