RetrofitManager

public class RetrofitManager {

    private final String baseUrl;
    private static RetrofitManager instance;
    private static OkHttpClient okHttpClient;

    //静态块
    static {
        getOkHttpClient();
    }

    private Retrofit retrofit;

    private RetrofitManager(String baseUrl){
        this.baseUrl = baseUrl;
        initRetrofit();
    }


    public static synchronized RetrofitManager getInstance(String baseUrl){
        if (instance==null){
            synchronized (RetrofitManager.class){
                if (null==instance){
                    instance = new RetrofitManager(baseUrl);
                }
            }
        }
        return instance;
    }

    public static OkHttpClient getOkHttpClient(){

        if (okHttpClient==null){
            synchronized (OkHttpClient.class){
                if (null==okHttpClient){
                    okHttpClient = new OkHttpClient.Builder()
                            .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
                            .connectTimeout(3000,TimeUnit.SECONDS)
                            .writeTimeout(3000,TimeUnit.SECONDS)
                            .readTimeout(3000,TimeUnit.SECONDS)
                            .build();
                }
            }
        }
        return okHttpClient;
    }

    private void initRetrofit(){
        retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .client(okHttpClient)
                .build();

    }

    public <T> T setCreate(Class<T> meq){
        return retrofit.create(meq);
    }
//retrofit2
    implementation 'com.squareup.retrofit2:retrofit:2.5.0'
    //rxjava2
    implementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0'
    //gson的解析器
    implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
    //logging-interceptor
    implementation 'com.squareup.okhttp3:logging-interceptor:3.10.0'
    //AndroidSchedulers
    implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值