MainActivity
public class MainActivity extends AppCompatActivity implements LoginView {
private EditText ed_pwd, ed_num;
private Button regist, login;
private LoginPresenter presenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//控件
ed_num = findViewById(R.id.num_login);
ed_pwd = findViewById(R.id.pwd_login);
login = findViewById(R.id.login_login);
regist = findViewById(R.id.regist_login);
//实例化p
presenter = new LoginPresenter(this);
//跳转注册
regist.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, RegistActivity.class));
finish();
}
});
//登录按钮
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取输入框的值
String phone = ed_num.getText().toString();
String pwd = ed_pwd.getText().toString();
boolean mobileNO = TelUtils.isMobileNO(phone);
//判断格式
if (!mobileNO) {
Toast.makeText(MainActivity.this, "手机号格式不对", Toast.LENGTH_SHORT);
return;
}
if (pwd.length() < 3) {
Toast.makeText(MainActivity.this, "密码格式不对", Toast.LENGTH_SHORT);
return;
}
Map<String, String> params = new HashMap<>();
params.put("phone", phone);
params.put("pwd", pwd);
presenter.onRelated(params);
}
});
}
@Override
public void getViewData(String status) {
if (status.equals("0000")) {
startActivity(new Intent(MainActivity.this, ShowActivity.class));
finish();
}
}
Presenter层
public class LoginPresenter {
private final LoginModel loginModel;
private final LoginView loginView;
public LoginPresenter(LoginView view) {
loginModel = new LoginModel();
loginView = view;
}
public void onRelated(Map<String, String> params) {
loginModel.getHttpData(params);
loginModel.setLoginListener(new LoginModel.onLoginListener() {
@Override
public void onResult(String status) {
loginView.getViewData(status);
}
});
View层
public interface LoginView {
void getViewData(String status);
}
utlis
public class OkHttpUtils {
//私有构造方法
private OkHttpUtils() {}
private static OkHttpUtils okHttpUtils = null;
//静态工厂
public static OkHttpUtils getInstance() {
if (okHttpUtils == null) {
okHttpUtils = new OkHttpUtils();
}
return okHttpUtils;
}
//拦截器
private static Interceptor myInterceptor() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
@Override
public void log(String message) {
Log.i("xxx", "log:拦截 " + message);
}
});
return interceptor;
}
//执行get方法
public static void doGet(String url, Callback callback) {
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(myInterceptor())
.build();
Request request = new Request.Builder()
.url(url)
.build();
okHttpClient.newCall(request).enqueue(callback);
}
//执行post
public static void doPost(String url, Map<String, String> params, Callback callback) {
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(myInterceptor())
.build();
//请求体
FormBody.Builder builder = new FormBody.Builder();
//遍历map集合
for (String key : params.keySet()) {
builder.add(key, params.get(key));
}
Request request = new Request.Builder()
.url(url)
.post(builder.build())
.build();
okHttpClient.newCall(request).enqueue(callback);
正则判断手机号
public class TelUtils {
public static boolean isMobileNO(String mobileNums) {
String telRegex = “((13[0-9])|(14[5,7,9])|(15[4])|(18[0-9])|(17[0,1,3,5,6,7,8]))\d{8}$”;
// "[1]“代表下一位为数字可以是几,”[0-9]“代表可以为0-9中的一个,”[5,7,9]"表示可以是5,7,9中的任意一位,[^4]表示除4以外的任何一个,\d{9}"代表后面是可以是0~9的数字,有9位。
if (TextUtils.isEmpty(mobileNums))
return false;
else
return mobileNums.matches(telRegex);
}
model
public class LoginModel {
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 0) {
String json = (String) msg.obj;
try {
JSONObject jsonObject = new JSONObject(json);
String status = jsonObject.getString(“status”);
loginListener.onResult(status);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
};
String path = “http://172.17.8.100/small/user/v1/login”;
public void getHttpData(Map<String, String> params) {
OkHttpUtils.doPost(path, params, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String json = response.body().string();
Message message = new Message();
message.what = 0;
message.obj = json;
handler.sendMessage(message);
}
});
}
//接口回调
public interface onLoginListener {
void onResult(String status);
}
public onLoginListener loginListener;
public void setLoginListener(onLoginListener loginListener) {
this.loginListener = loginListener;
}