仿京东注册接口

注册model

public class RegisterModel {


    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    };
    private UserBean bean;


    public void register(String mobile,String pwd, final RegCallBack regCallBack){

          OkHttpClient client = new OkHttpClient.Builder()
                  .build();

          FormBody formBody = new FormBody.Builder()
                  .add("mobile",mobile)
                  .add("password",pwd)
                  .build();

          final Request request = new Request.Builder()
                  .url(API.REG_URL)
                  .post(formBody)
                  .build();

          client.newCall(request).enqueue(new Callback() {
              @Override
              public void onFailure(Call call, IOException e) {

                  if(regCallBack!=null){
                      regCallBack.failure("请求失败");
                  }

              }

              @Override
              public void onResponse(Call call, Response response) throws IOException {

                 if(response.code()==200){
                     String s = response.body().string();
                     JsonRequest(s,regCallBack);
                 }
              }
          });


      }

    private void JsonRequest(String s, final RegCallBack regCallBack) {

          if(s!=null){

              bean = new Gson().fromJson(s,UserBean.class);
             handler.post(new Runnable() {
                 @Override
                 public void run() {
                     regCallBack.success(bean);
                 }
             });

          }


      }


    public interface RegCallBack{
          void failure(String errorMsg);

          void success(UserBean userBean);
      }

注册View

 interface IRview {

    void mobileVerify();//检测手机号
    void mobileEmpty();//检测手机号是否为空
    void pwdVerify(); //检测密码

    //请求成功
    void success(UserBean userBean);
    //请求失败
    void failure(String msg);

注册控制器

private IRview iRview;
private RegisterModel registerModel;

public RegisterPresenter(IRview iRview) {
    this.iRview = iRview;
    this.registerModel = new RegisterModel();
}


public void register(String mobile,String pwd){

    //手机号为空
    if(TextUtils.isEmpty(mobile)){
        iRview.mobileEmpty();
        return;
    }
    //手机号格式
    if(RegexValidateUtil.checkCellphone(pwd)){
        iRview.mobileVerify();
        return;
    }
    //密码为空
    if(TextUtils.isEmpty(pwd)){
        iRview.pwdVerify();
        return;
    }

    registerModel.register(mobile, pwd, new RegisterModel.RegCallBack() {
        @Override
        public void failure(String errorMsg) {
            iRview.failure(errorMsg);
        }

        @Override
        public void success(UserBean userBean) {

            iRview.success(userBean);
        }
    });


测试注册
private EditText mobile;
private EditText pwd;
private RegisterPresenter presenter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);
    initData();
    initView();
}

private void initView() {

    mobile = findViewById(R.id.edit_phone);
    pwd = findViewById(R.id.edit_pwd);

}

private void initData() {

    presenter = new RegisterPresenter(this);

}

public void register(View view) {

 presenter.register(mobile.getText().toString(),pwd.getText().toString());

}



@Override
public void mobileVerify() {

    Toast.makeText(this,"手机号码必须是11位",Toast.LENGTH_LONG).show();

}

@Override
public void mobileEmpty() {
    Toast.makeText(this,"手机号码不能为空",Toast.LENGTH_LONG).show();
}

@Override
public void pwdVerify() {
    Toast.makeText(this,"密码格式不正确",Toast.LENGTH_LONG).show();
}

@Override
public void success(UserBean userBean) {
    Toast.makeText(this,userBean.msg,Toast.LENGTH_LONG).show();
    Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
    startActivity(intent);
}

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

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值