1.純屬方便自己使用
2.將網絡請求的業務代碼和UI展示的代碼分離,用時就調用。
3.接口請求複用。
/**
* @Auther: xiexibo
* @Date: 2019/3/16 10:56:28
* @Description:RetrofitUtils網絡請求工具類
*/
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;
}
//獲取Retrofit方法
private static Retrofit retrofit;
private static synchronized Retrofit getRetrofit(String BASE_URL) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit;
}
//get方法
public <T> T doGet(String BASE_URL, Class<T> apiService) {
//獲取Retrofit
Retrofit retrofit = getRetrofit(BASE_URL);
//通過Java動態代理的方式獲取動態代理對象
T t = retrofit.create(apiService);
return t;
}