retrofit+okhttp+Rxjava封装

public class RetrofitFactory {
    private static volatile RetrofitFactory instance;
            private  ApiService apiService;

            public static OkHttpClient okHttpClient = new OkHttpClient.Builder()
                    .connectTimeout(2000, TimeUnit.SECONDS)
                    .readTimeout(2000, TimeUnit.SECONDS)
                    .writeTimeout(2000, TimeUnit.SECONDS)
                    .build();
           public RetrofitFactory(String baseUrl) {
                    //使我们能高度自定义转化器
                    //把 以前的 call 转化成 Observable,这是RetrofitRxJava结合使用的关键
                    Retrofit retrofit = new Retrofit.Builder()
                            .baseUrl(baseUrl)
                            //使我们能高度自定义转化器
                            .addConverterFactory(ScalarsConverterFactory.create())
                            .client(okHttpClient)
                            //把 以前的 call 转化成 Observable,这是RetrofitRxJava结合使用的关键
                            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                            .addConverterFactory(GsonConverterFactory.create())
                            .build();
                   apiService = retrofit.create(ApiService.class);

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

            public Observable get(String url, Map, String> map) {


                return  apiService.get(url, map)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread());
            }

           

}

你可能感兴趣的:(retrofit+okhttp+Rxjava封装)