public static RetorfitUtils mInstance;
private Retrofit retrofit;
//单例模式
/* private static final class SINGLE_INSTANCE {
public static final RetroUtils _INSTANCE = new RetroUtils();
}*/
public static RetorfitUtils getInstance() {
if (mInstance == null) {
mInstance = new RetorfitUtils();
}
return mInstance;
}
public RetorfitUtils() {
retrofit = new Retrofit.Builder()
.baseUrl(MyApi.BASE_URI)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(buildOkhttpClient())
.build();
}
//设置读写超时
private OkHttpClient buildOkhttpClient() {
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);//打印请求参数,请求结果
return new OkHttpClient.Builder()
.addInterceptor(httpLoggingInterceptor)
.writeTimeout(3000, TimeUnit.MILLISECONDS)
.readTimeout(3000, TimeUnit.MILLISECONDS)
.build();
}
public <T> T create(Class<T> clazz) {
return retrofit.create(clazz);
}