注册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(); }