一个小单例

版权声明:本文为宠歆小王子的原创文章,未经宠歆小王子允许不得转载。 https://blog.csdn.net/qq_41698379/article/details/84670528
//懒汉式

private static volatile HttpUtils instance;

    private OkHttpClient client;

    private Handler handler = new Handler(Looper.getMainLooper());
//这里面我加上了日志拦截器
    private HttpUtils() {
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        client = new OkHttpClient.Builder()
                .connectTimeout(5, TimeUnit.SECONDS)
                .addInterceptor(interceptor)
                .build();
    }
    public static HttpUtils getInstance() {
        if (instance == null) {
            synchronized (HttpUtils.class) {
                if (null == instance) {
                    instance = new HttpUtils();
                }
            }
        }
        return instance;
    } 
}

猜你喜欢

转载自blog.csdn.net/qq_41698379/article/details/84670528