1. 程式人生 > >封裝retrofit和rxjava1.0的操作

封裝retrofit和rxjava1.0的操作

public class Fengzhuang {
    public static Fengzhuang instance;
    private Retrofit retrofit;
    private  ApiRetrofit api;    public ApiRetrofit getApi() {
        return api;
    }    private  Fengzhuang() {
        //初始化Retrofit
        //給Retrofit新增Gson解析功能
//給Retrofit新增Rxjava功能
        retrofit = new Retrofit.Builder().baseUrl("https://gank.io/api/data/%E7%A6%8F%E5%88%A9/")
                //給Retrofit新增Gson解析功能
                .addConverterFactory(GsonConverterFactory.create())
                //給Retrofit新增Rxjava功能
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        //初始化Retrofit的Api介面
        api = retrofit.create(ApiRetrofit.class);
    }    //執行緒鎖單例
    public static Fengzhuang getInstance() {
        if (instance == null) {
            synchronized (Fengzhuang.class) {
                if (instance == null) {
                    instance = new Fengzhuang();
                }
            }
        }
        return instance;
    }
    public static OkHttpClient okHttpClient1=new OkHttpClient.Builder()
            .connectTimeout(20, TimeUnit.SECONDS)
            .readTimeout(20,TimeUnit.SECONDS)
            .writeTimeout(20,TimeUnit.SECONDS)
            .addInterceptor(new LoggingInterceptor())
            .build();    public static ApiRetrofit apiRetrofitnew= new Retrofit.Builder()
            .baseUrl("http://qbh.2dyt.com")
            .addConverterFactory(ScalarsConverterFactory.create())
            .client(okHttpClient1)
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .build().create(ApiRetrofit.class);
    public static io.reactivex.Observable<String> get(String url) {
        return apiRetrofitnew.get(url)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }
    public static io.reactivex.Observable<String> get(String url, Map<String, String> map) {
        return apiRetrofitnew.get(url, map).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }
    public static io.reactivex.Observable<String> post(String url, Map<String, String> map) {
        return apiRetrofitnew.post(url, map).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }
    //網路請求日誌攔截器,直接拷貝使用即可
    static class LoggingInterceptor implements Interceptor {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();            long t1 = System.nanoTime();
            KLog.i(String.format("Sending request %s on %s%n%s",
                    request.url(), chain.connection(), request.headers()));            Response response = chain.proceed(request);            long t2 = System.nanoTime();
            KLog.i(String.format("Received response for %s in %.1fms%n%s",
                    response.request().url(), (t2 - t1) / 1e6d, response.headers()));            return response;
        }
    }}