MVP契约 写 "登录"

1.写依赖

//Butterknife一键生成ID 的依赖
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
//OKHttp的依赖
implementation 'com.squareup.okhttp3:okhttp:3.12.1'

2.分包
在这里插入图片描述
3.写一个常量类来放接口

public class Apils {
//登录的URL
public static final String LOGIN_URL = "http://172.17.8.100/small/user/v1/login";

}

4.契约Contract

//这里面都是接口  是MVP的接口
public interface ILoginContract {

//接口里可以定义接口

//首先定义V层接口
public interface ILoginView{
    public  void  showData(String message);
}

//定义P层接口
public interface ILoginPresenter<ILoginView>{

        //进行绑定
        public void attachView(ILoginView iLoginView);

        //进行解绑
        public void detachView(ILoginView iLoginView);

        //数据请求  请求M层数据   做登录
        public  void  requestLoginData(String userName, String password);

}

//M层的接口
public interface ILoginModel{

    //登录的接口  并  请求
    public void containLoginResponesData(String userName, String password,MyCallBack callBack);

    //接口回调

    public interface MyCallBack{

        //回显数据方法
        public void responseData(String message);
    }
}}

5.LoginActivity继承V层

public class LoginActivity extends AppCompatActivity implements ILoginContract.ILoginView {

@butterknife.BindView(R.id.ed_phone)
EditText edPhone;
@butterknife.BindView(R.id.ed_pwd)
EditText edPwd;
@butterknife.BindView(R.id.btn_login)
Button btnLogin;
private ILoginContract.ILoginPresenter presenter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    butterknife.ButterKnife.bind(this);
    //创建P层的实现类
    presenter = new LoginPresenterImpl();
    //实现P层的方法
    presenter.attachView(this);
}

@butterknife.OnClick(R.id.btn_login)
public void onViewClicked() {
    //获取用户名  和密码
    String userName = edPhone.getText().toString();
    String password = edPwd.getText().toString();
    //获取P层的接口  进行操作  并传入参数  没有P层  那就创建一个P层
    //带个参数走

    presenter.requestLoginData(userName,password);
}

@Override
public void showData(final String message) {
    //运行在UI线程
  runOnUiThread(new Runnable() {
      @Override
      public void run() {
          Toast.makeText(LoginActivity.this, message, Toast.LENGTH_SHORT).show();
      }
  });
}

@Override
protected void onDestroy() {
    super.onDestroy();
    //解绑
    presenter.detachView(this);
}}

6.P层

public class LoginPresenterImpl implements ILoginContract.ILoginPresenter<ILoginContract.ILoginView> {
//在这里实现V层和M层
ILoginContract.ILoginView loginView;
private ILoginContract.ILoginModel modelImpl;
private SoftReference<ILoginContract.ILoginView> reference;

@Override
public void attachView(ILoginContract.ILoginView iLoginView) {
    this.loginView=iLoginView;
    //在这里要 new 一个M层 的实现类
    modelImpl = new LoginModelImpl();
    //之后调用一个软引用 进行包裹
    reference = new SoftReference<>(loginView);

}

@Override
public void detachView(ILoginContract.ILoginView iLoginView) {
    //清除
    reference.clear();
}

@Override
public void requestLoginData(String userName, String password) {
    modelImpl.containLoginResponesData(userName, password, new ILoginContract.ILoginModel.MyCallBack() {
        //回传回来的数据
        @Override
        public void responseData(String message) {

            //V层  把这个数据接走  节奏的数据是
            loginView.showData(message);
        }
    });
}}

7.M层

public class LoginModelImpl implements ILoginContract.ILoginModel {

@Override
public void containLoginResponesData(String userName, String password, MyCallBack callBack) {
    //调用方法
    //注意参数
    requestLoginDataEnqueue(userName,password, callBack);
}

//在这个方法中有一个 MyCallBack   用这个在M层向P层传递数据
private void requestLoginDataEnqueue(String userName, String password, final MyCallBack callBack) {

    //创建OKHTTP
    OkHttpClient client = new OkHttpClient.Builder().build();

    //得到一个  空的表单请求体
    FormBody body = new FormBody.Builder().build();

    //获取Request 对象
    Request request = new Request.Builder()
            //请求方式
            .method("POST",body)
            //请求地址
            .url(Apils.LOGIN_URL+"?phone="+userName+"&pwd="+password)
            .build();

    //开启请求
    Call call = client.newCall(request);
    //异步请求
     call.enqueue(new Callback() {
         @Override
         public void onFailure(Call call, IOException e) {

             //错误数据
             String message = e.getMessage();
             //接口回调回传数据
             callBack.responseData(message);
         }

         @Override
         public void onResponse(Call call, Response response) throws IOException {
             //想对应数据
             String request = response.body().string();
             callBack.responseData(request);
         }
     });
}}

猜你喜欢

转载自blog.csdn.net/weixin_43860442/article/details/85425090