转载别人的RetrofitUtils工具类

public class Httputils {

    private final OkHttpClient build;
    private static Httputils httputils;

    private Httputils(){

        build = new OkHttpClient.Builder()
                .addInterceptor(new LogginInstance())
                .writeTimeout(5,TimeUnit.SECONDS)
                .readTimeout(5,TimeUnit.SECONDS)
                .connectTimeout(5,TimeUnit.SECONDS)
                .build();
    }
    class LogginInstance implements Interceptor{

        @Override
        public Response intercept(Chain chain) throws IOException {
            //请求
            Request request = chain.request();
            //获取地址
            String method = request.method();
            //获取路径
            HttpUrl url = request.url();

            Log.d("LogginInstance", method + "" + url);

            Response proceed = chain.proceed(request);
            return proceed;
        }
    }

    public  T creatApi(Class cls){
        Retrofit build = new Retrofit.Builder()
                .client(this.build)
                .baseUrl(Contance.BASE_USER)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        return build.create(cls);
    }

    public static Httputils getInstanse(){
        if (httputils==null){
            synchronized (Httputils.class){
                if (httputils==null){
                    httputils=new Httputils();
                }
            }
        }
        return  httputils;
    }

}

 

你可能感兴趣的:(转载别人的RetrofitUtils工具类)