Retrofit的封装类


public class RetrofitHelper {

    private static OkHttpClient okHttpClient;

    private static ShopApi shopApi;

    static {
        initOkHttpClient();
    }

    /**
     * 初始化 OkHttpClient
     */
    private static void initOkHttpClient() {

        if (okHttpClient == null) {
            synchronized (RetrofitHelper.class) {
                if (okHttpClient == null) {
                    okHttpClient = new OkHttpClient.Builder()
                            .build();
                }
            }
        }

    }

    /**
     * 定义一个泛型方法
     *
     * @param clazz
     * @param url
     * @param <T>
     * @return
     */
    public static <T> T createAPI(Class<T> clazz, String url) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(url)
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        return retrofit.create(clazz);
    }

    public static ShopApi getShopAPI() {
        if (shopApi == null) {
            synchronized (RetrofitHelper.class) {
                if (shopApi == null) {
                    shopApi = createAPI(ShopApi.class, Constants.BASE_URL);
                }
            }
        }
        return shopApi;
    }

}

posted @ 2017-12-05 15:10  Ars灬木子  阅读(275)  评论(0编辑  收藏  举报