public class RetrofitHelper { private static volatile OkHttpClient okHttpClient; private static volatile Retrofit retrofit; static { initHttp(); } private static void initHttp() { if (okHttpClient == null) { synchronized (RetrofitHelper.class) { if (okHttpClient == null) { HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); logging.setLevel(HttpLoggingInterceptor.Level.BODY); okHttpClient = new OkHttpClient.Builder() .addInterceptor(new GetParamsInterceptor()) .addInterceptor(logging) .addNetworkInterceptor(new StethoInterceptor()) .build(); } } } } private static Retrofit getRetrofit() { if (retrofit == null) { synchronized (RetrofitHelper.class) { if (retrofit == null) { retrofit = new Retrofit.Builder() .client(okHttpClient) .baseUrl(Constants.BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build(); } } } return retrofit; } private static <T> T createApi(Class<T> clazz){ return getRetrofit().create(clazz); } public static HomeApi getHomeApi() { return createApi(HomeApi.class); } public static ShootApi getShootApi() { return createApi(ShootApi.class); } public static SearchApi getSearchApi() { return createApi(SearchApi.class); } }
简单的封装RetrofitHelper
最新推荐文章于 2024-05-08 11:36:42 发布