MVP实现登录和注册
程序员文章站
2022-04-18 17:24:52
...
/////////////////////model
package com.example.ggq.gaoguoqing20180811.model;
import android.os.Handler;
import android.os.Message;
import com.example.ggq.gaoguoqing20180811.bean.UserBean;
import com.example.ggq.gaoguoqing20180811.common.Api;
import com.example.ggq.gaoguoqing20180811.utils.OkHttpUtil;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.HashMap;
import okhttp3.Response;
public class RegModel {
private RegCallback callback;
public void setCallback(RegCallback callback) {
this.callback = callback;
}
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
}
};
public void register(HashMap<String,String> params, final RegCallback regCallback){
OkHttpUtil.getInstance().postData(Api.REG_URL, params, new OkHttpUtil.RequestCallback() {
@Override
public void onSuccess(Response response) {
if(response.code() == 200){
try {
String result = response.body().string();
parseJsonResult(result,regCallback);
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void onError(IOException e) {
if(regCallback != null){
regCallback.failure("数据请求失败!");
}
}
});
}
private void parseJsonResult(String result, final RegCallback regCallback) {
if(result != null){
final UserBean userBean = new Gson().fromJson(result, UserBean.class);
handler.post(new Runnable() {
@Override
public void run() {
if(regCallback != null){
regCallback.success(userBean);
}
}
});
}
}
public interface RegCallback{
void success(UserBean userBean);
void failure(String errorMsg);
}
}
////////////////////////////////////////////////////////////////////////////////view
package com.example.ggq.gaoguoqing20180811.view; import com.example.ggq.gaoguoqing20180811.bean.UserBean; public interface RegView { void mobileVerify();//手机号合法 void pwdVerify();//密码合法 void mobileEmpty();//手机号非空验证 void pwdEmpty();//密码非空验证 void onSuccess(UserBean userBean);//数据请求成功 void onError(String errorMsg);//数据请求失败 }
///////////MainActivity
package com.example.ggq.gaoguoqing20180811; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.EditText; import android.widget.Toast; import com.example.ggq.gaoguoqing20180811.bean.UserBean; import com.example.ggq.gaoguoqing20180811.presenter.RegPresenter; import com.example.ggq.gaoguoqing20180811.view.RegView; public class MainActivity extends AppCompatActivity implements RegView { private EditText edit_tel; private EditText edit_pwd; private RegPresenter regPresenter; private String tel; private String pwd; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); regPresenter = new RegPresenter(this); } private void initView() { edit_tel = findViewById(R.id.edit_tel); edit_pwd = findViewById(R.id.edit_pwd); } @Override public void mobileVerify() { Toast.makeText(this, "手机号必须是11位合法的手机号", Toast.LENGTH_SHORT).show(); } @Override public void pwdVerify() { Toast.makeText(this, "密码必须包含小写字母,数字,可以是字母数字下划线组成并且长度是6到16", Toast.LENGTH_SHORT).show(); } @Override public void mobileEmpty() { Toast.makeText(this, "手机号不能为空", Toast.LENGTH_SHORT).show(); } @Override public void pwdEmpty() { Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show(); } @Override public void onSuccess(UserBean userBean) { Toast.makeText(this, userBean.msg, Toast.LENGTH_SHORT).show(); Intent intent = new Intent(this, LoginActivity.class); intent.putExtra("tel",tel); intent.putExtra("pwd",pwd); startActivity(intent); finish(); } @Override public void onError(String errorMsg) { Toast.makeText(this, errorMsg, Toast.LENGTH_SHORT).show(); } public void reg(View view) { tel = edit_tel.getText().toString(); pwd = edit_pwd.getText().toString(); regPresenter.register(tel, pwd); } @Override protected void onDestroy() { super.onDestroy(); if(regPresenter != null){ regPresenter.onDetach(); } } }
/////////////////////presenter
package com.example.ggq.gaoguoqing20180811.presenter;
import android.text.TextUtils;
import com.example.ggq.gaoguoqing20180811.bean.UserBean;
import com.example.ggq.gaoguoqing20180811.model.RegModel;
import com.example.ggq.gaoguoqing20180811.utils.VerifyUser;
import com.example.ggq.gaoguoqing20180811.view.RegView;
import java.util.HashMap;
public class RegPresenter {
private RegModel regModel;
private RegView regView;
public RegPresenter(RegView regView) {
onAttach(regView);
this.regModel = new RegModel();
}
public void onAttach(RegView regView){
this.regView = regView;
}
public void onDetach(){
this.regView = null;
}
public void register(String mobile,String pwd){
if(TextUtils.isEmpty(mobile)){
regView.mobileEmpty();
return;
}
if(!VerifyUser.isMobileNO(mobile)){
regView.mobileVerify();
return;
}
if(TextUtils.isEmpty(pwd)){
regView.pwdEmpty();
return;
}
if(!VerifyUser.isPasswordNo(pwd)){
regView.pwdVerify();
return;
}
HashMap<String, String> params = new HashMap<>();
params.put("mobile",mobile);
params.put("password",pwd);
regModel.register(params,new RegModel.RegCallback() {
@Override
public void success(UserBean userBean) {
regView.onSuccess(userBean);
}
@Override
public void failure(String errorMsg) {
regView.onError(errorMsg);
}
});
}
}