The MVP architecture consists of Model, Presenter, and View.

Model represents Model, mainly responsible for data loading; VIew is mainly responsible for VIew binding, interface display, interface logic jump, etc. A Presenter is a Presenter that acts as a middleman between the View and the Model to implement the idea of high cohesion and low coupling.

MVP adds a lot of interfaces and classes, which makes it hard to manage, and that’s where Contract comes in. A. Contract B. Contract C. The contract; The agreement. Contract: A Contract that manages models, views, and presenters to facilitate class search and maintenance.

To add a dependency:

/ / rxjava depend on implementation 'IO. Reactivex. Rxjava2: rxjava: 2.1.9' implementation 'IO. Reactivex. Rxjava2: rxandroid: 2.0.2' / / retrofit depend on 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'Copy the code

Create API request path: ApiService

@get ("wenda/comments") Observable<Object> getList(@field ("name") String name); }Copy the code

Create a BaseContact definition interface model

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

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

    interface BaseModel {

    }
}
Copy the code

Create the MVP definition interface for each layer:

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); }}Copy the code

Create layer M implementation: 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()); }}Copy the code

Create layer P implementation: 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(" request succeeded "); } } }, new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { if (mView ! = null) {mview. showToast(" network exception "); }}}); } @Override public void attach(BaseContact.BaseView view) { this.mView = (UserListContact.UserListView) view; } @Override public void detach() { this.mView = null; }}Copy the code

Define a Retrofit request:

public class ApiManager { public static ApiService getApiService(Retrofit retrofit) { return retrofit.create(ApiService.class); }}Copy the code

Create a Retrofit network request:

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 adapter AddCallAdapterFactory (RxJava2CallAdapterFactory. The create ()) / / GSON parsing .addConverterFactory(GsonConverterFactory.create()) .build(); } public static Retrofit getManager() { if (mManager == null) { synchronized (RetrofitManager.class) { if (mManager == null) { mManager = new RetrofitManager(); } } } return mManager.mRetrofit; }}Copy the code

MainActivity code:

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 (" game "); } @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(); }}Copy the code