他人のRetrofitUtilsツール類を転載する

1602 ワード

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;
    }

}