Retrofit+RxJava备忘

本文介绍如何使用Retrofit 2.0结合RxJava实现高效的网络请求处理。通过创建RetrofitManager工具类,实现了单例模式下的Retrofit实例化,并集成了Gson解析器与RxJava响应适配器。示例展示了M层调用流程,包括Observable的创建、订阅、调度器的设置及UI线程更新。同时提供了接口Bean类定义示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

仅作备忘。

 

//配置retrofit2.0
    compile 'com.squareup.retrofit2:retrofit:+'
    compile 'com.squareup.retrofit2:converter-gson:+'
 
//rxjava
compile 'com.squareup.retrofit2:adapter-rxjava2:+'
compile 'io.reactivex.rxjava2:rxjava:+'
compile 'io.reactivex.rxjava2:rxandroid:+'

retroft + rxjava 工具类

public class RetrofitManager {

    private final Retrofit mRetrofit;
    private static final String BASE_URL = "baseURl,以/结尾";
    private static final class SINGLE_INSTENCE {
        private static final RetrofitManager INSTENCE = new RetrofitManager();
    }


    public static RetrofitManager getInstence() {
        return SINGLE_INSTENCE.INSTENCE;
    }

    public RetrofitManager() {
        mRetrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .client(buildOkHttpClient())
                .build();
    }

    private OkHttpClient buildOkHttpClient(){
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY);

       return new OkHttpClient.Builder()
               .addInterceptor(interceptor)
                .connectTimeout(5, TimeUnit.SECONDS)
                .readTimeout(5,TimeUnit.SECONDS)
                .readTimeout(5,TimeUnit.SECONDS)
                .build();
    }

    public <T> T create(Class<T> tClass) {
        return mRetrofit.create(tClass);
    }
}

 M层调用,


public Observable<LoginBean(登录的bean类)> login(String mobile, String password){
        ILoginBean iLoginBean = RetrofitManager.getInstence().create(ILoginBean.class);//拼接的Bean类
        return iLoginBean.login(mobile,password);
    }

 p层传值后,分配工作,ui线程:

 model.reg(mobile,password)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<RegBean>() {
                    @Override
                    public void accept(RegBean regBean) throws Exception {
                        Log.d(TAG, "accept: "+ regBean.getMsg());
                        if (iView != null){
                            if (regBean != null){
                                iView.onSuccess(regBean);
                            }

                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        if (iView != null){
                            String message = throwable.getMessage();
                            iView.onFaild(message);
                        }
                    }
                });

 

接口bean类     

@GET("user/reg")
    Observable<RegBean> reg(@Query("mobile") String mobile, @Query("password") String password);

导包非常容易不在意,

io.reactivex.Observable包下
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值