阅读 144

MVP+RxJava+Retrofit框架

MVP架构是由Model、Presenter、View这三个组成。

Model表示模型,主要负责数据的加载;VIew表示视图主要负责视图绑定,界面的展示,界面逻辑跳转等;Presenter是表示器,主要作为View和Model的中间人,完成他们之间的交互,从而实现高内聚低耦合的思想。

MVP在实现代码简洁的同时,额外增加了大量的接口、类,不方便进行管理,于是Contract就登场了。 Contract: 百度翻译 : 合同;契约;协议。 Contract: 如其名,是一个契约,将Model、View、Presenter 进行约束管理,方便后期类的查找、维护。

7c10784b2e877730f241fc04ca87515.png

进行添加依赖:

//rxjava依赖
implementation 'io.reactivex.rxjava2:rxjava:2.1.9'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'

//retrofit依赖
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation 'com.squareup.okhttp3:okhttp:3.10.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.9.1'
复制代码

创建api请求路径:ApiService

public interface ApiService {

    // 请求路径不要加 斜杠
    @GET("wenda/comments")

    Observable<Object> getList(@Field("name") String name);

}
复制代码

创建BaseContact定义接口模范

public interface BaseContact {
    interface BaseView {
        void showToast(String msg);
    }

    interface BasePresenter {
        void attach(BaseView view);
        void detach();
    }

    interface BaseModel {

    }
}
复制代码

创建MVP每层的定义接口:

public interface UserListContact {

    interface UserListView extends BaseContact.BaseView {
        void setText(String toString);
    }

    interface UserListPresenter extends BaseContact.BasePresenter {
        void getList(String name);
    }

    interface UserListModel {
        Observable<Object> getList(String name);
    }

}
复制代码

创建M层实现:UserListModelImpl:

public class UserListModelImpl implements UserListContact.UserListModel{


    @Override
    public Observable<Object> getList(String name) {
        return ApiManager.getApiService(RetrofitManager.getManager())
                .getList(name)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }


}
复制代码

创建P层实现:UserListPresenterImpl:

public class UserListPresenterImpl  implements UserListContact.UserListPresenter{

    private UserListModelImpl mModel;
    private UserListContact.UserListView mView;

    public UserListPresenterImpl() {
        mModel = new UserListModelImpl();
    }

    @Override
    public void getList(String name) {
        mModel.getList(name)
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        if (mView != null) {
                            mView.setText(o.toString());
                            mView.showToast("请求成功");
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        if (mView != null) {
                            mView.showToast("网络异常");
                        }
                    }
                });
    }


    @Override
    public void attach(BaseContact.BaseView view) {
        this.mView = (UserListContact.UserListView) view;
    }

    @Override
    public void detach() {
        this.mView = null;
    }
}
复制代码

定义Retrofit请求:

public class ApiManager {

    public static ApiService getApiService(Retrofit retrofit) {
        return retrofit.create(ApiService.class);
    }

}
复制代码

创建Retrofit网络请求:

public class RetrofitManager {

    private final String BASE_URL = "http://wanandroid.com/";

    private volatile static RetrofitManager mManager;
    private Retrofit mRetrofit;

    private RetrofitManager() {
        mRetrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                // RXJava 适配器
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                // GSON 解析
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    public static Retrofit getManager() {
        if (mManager == null) {
            synchronized (RetrofitManager.class) {
                if (mManager == null) {
                    mManager = new RetrofitManager();
                }
            }
        }
        return mManager.mRetrofit;
    }
}
复制代码

MainActivity代码:

public class MainActivity extends AppCompatActivity implements UserListContact.UserListView {

    private UserListContact.UserListPresenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mPresenter = new UserListPresenterImpl();
        mPresenter.attach(this);

        mPresenter.getList("游戏");
    }

    @Override
    public void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
    }

    @Override
    public void setText(String toString) {
//        text.setText(toString);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.detach();
    }
}
复制代码
文章分类
Android
文章标签