android项目实战之二十一之Retrofit简单封装使用

目标

实现网络请求,目前大部分都考虑到第三方库okhttp、Retrofit、RxJava等

Retrofit封装

******************核心代码 
private RetrofitHelper() {
        //默认BASE_URL赋值
        baseUrl = "自己的";
        String timeString = UserManager.tokeToken(MyRealApplication.getApplication());
        //构建OkHttpClient对象
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        //日志拦截器
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        builder.addInterceptor(loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY))
                .addInterceptor(getRequestInterceptor())
                .addInterceptor(getHeaderInterceptor())
                .addInterceptor(new TokenInterceptor(timeString))
                .connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(45, TimeUnit.SECONDS)
                .writeTimeout(45, TimeUnit.SECONDS)
                .retryOnConnectionFailure(true);
        OkHttpClient okHttpClient = builder.build();

        //创建retrofit构建者对象
        retrofitBuilder = new Retrofit.Builder();
        retrofitBuilder.client(okHttpClient)
                .addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create());
    }

    public static RetrofitHelper getInstance() {
        if (instance == null) {
            synchronized (RetrofitHelper.class) {
                if (instance == null) {
                    instance = new RetrofitHelper();
                }
            }
        }
        return instance;
    }
*****************************************核心代码

使用方式 

 RetrofitHelper
                        .getInstance()
                        .getRetrofit()
                        .create(Api.class)
                        .addmember(params)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Observer>() {
                            @Override
                            public void onSubscribe(Disposable d) {

                            }

                            @Override
                            public void onNext(ResponseModel integerResponseModel) {
                                Log.d("addmember", String.valueOf(integerResponseModel));
                                Log.d("addmember", integerResponseModel.getData().toString());
                                Intent intent = new Intent(activity, QingActivity.class);
                                Bundle bundle=new Bundle();
                                bundle.putInt("uId",integerResponseModel.getData());
                                bundle.putString("name",keywords);
                                intent.putExtras(bundle);
                                startActivity(intent);
                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onComplete() {
                                getUserList(keywords);
                            }
                        });

你可能感兴趣的:(android,retrofit)