Rxjava+Retrofit+mvp 封装

1.RetrofitUtils
public class RetrofitUtils {
    private static volatile RetrofitUtils instance;
    private final Retrofit retrofit;

    private RetrofitUtils(String baseurl) {
        retrofit = new Retrofit.Builder()
                .baseUrl(baseurl)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

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

    public Retrofit getretrofit(){
        return retrofit;
    }
}
2.ApiService
public interface ApiService {
    //http://120.27.23.105/product/getCarts  购物车
    @GET("product/getCarts")
    Flowable<CarBean> getcars(@QueryMap Map<String,String> map);

    //http://120.27.23.105/product/deleteCart   删除
    @GET("product/deleteCart")
    Flowable<delBean> delete(@QueryMap Map<String,String> map);
}

3.IModel
public interface IModel {
    void get(String baseurl, Map<String, String> map, String tag);
}
4.Model
public class Model implements IModel {
    private MyPresenter presenter;

    public Model(MyPresenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void get(String baseurl, Map<String, String> map, String tag) {
        ApiService service = RetrofitUtils.getInstance(baseurl).getretrofit().create(ApiService.class);
        if (tag.equals("getcars")){//购物车
                Flowable<CarBean> flowable = service.getcars(map);
                presenter.getData(flowable,tag);
        }else if (tag.equals("del")){//删除
            Flowable<delBean> flowable = service.delete(map);
            presenter.getData(flowable,tag);
        }
    }
}

5.IPresenter
public interface IPresenter {
    void get(String baseurl, Map<String, String> map, String tag);
}
6.MyPresenter
public class MyPresenter implements IPresenter{
    private IView iv;
    private DisposableSubscriber subscriber;

    public MyPresenter(IView iv) {
        this.iv = iv;
    }

    @Override
    public void get(String baseurl, Map<String, String> map, String tag) {
        Model model = new Model(this);
        model.get(baseurl,map,tag);
    }

    public void getData(Flowable flowable, final String tag){
        subscriber = (DisposableSubscriber) flowable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableSubscriber() {
                    @Override
                    public void onNext(Object o) {
                        iv.onSuccess(o,tag);
                    }

                    @Override
                    public void onError(Throwable t) {
                        iv.onFailed((Exception) t);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    //防止内存泄漏
    public void detatch(){
        if (iv != null) {
            iv = null;
        }
        if(subscriber !=null){
            if(!subscriber.isDisposed()){
                subscriber.dispose();
            }
        }
    }
}


7.IView
public interface IView {
    void onSuccess(Object o, String tag);
    void onFailed(Exception e);
}










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于Android项目中的网络请求,RxJavaRetrofitMVP是常用的框架组合。下面是一个简单的网络框架封装示例: 首先,在项目中引入RxJavaRetrofit的依赖。 ``` implementation 'io.reactivex.rxjava2:rxjava:2.2.19' implementation 'io.reactivex.rxjava2:rxandroid:2.1.1' implementation 'com.squareup.retrofit2:retrofit:2.9.0' implementation 'com.squareup.retrofit2:converter-gson:2.9.0' implementation 'com.squareup.retrofit2:adapter-rxjava2:2.9.0' ``` 然后,创建一个Retrofit的单例类,用于进行网络请求的初始化和配置。 ```java public class RetrofitClient { private static Retrofit retrofit; private static final String BASE_URL = "https://api.example.com/"; public static Retrofit getClient() { if (retrofit == null) { retrofit = new Retrofit.Builder() .baseUrl(BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build(); } return retrofit; } } ``` 接下来,创建一个ApiService接口,定义网络请求的方法。 ```java public interface ApiService { @GET("users") Observable<List<User>> getUsers(); } ``` 然后,创建一个DataManager类,用于管理网络请求。 ```java public class DataManager { private ApiService apiService; public DataManager() { apiService = RetrofitClient.getClient().create(ApiService.class); } public Observable<List<User>> getUsers() { return apiService.getUsers(); } } ``` 最后,在MVP的Presenter中调用DataManager类进行网络请求。 ```java public class UserPresenter { private UserView userView; private DataManager dataManager; public UserPresenter(UserView userView) { this.userView = userView; dataManager = new DataManager(); } public void getUsers() { dataManager.getUsers() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer<List<User>>() { @Override public void onSubscribe(Disposable d) { // 在请求开始时的操作 } @Override public void onNext(List<User> users) { // 请求成功返回数据时的操作 userView.showUsers(users); } @Override public void onError(Throwable e) { // 请求失败时的操作 userView.showError(e.getMessage()); } @Override public void onComplete() { // 请求完成时的操作 } }); } } ``` 这样,就完成了一个简单的Android RxJava + Retrofit + MVP网络框架封装。你可以根据自己的需要,进行进一步的封装和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值