//在开始之前导入okhttp和recyclerview依赖
implementation ‘com.squareup.okhttp3:okhttp:3.2.0’
implementation ‘com.squareup.okio:okio:1.7.0’
implementation ‘com.android.support:recyclerview-v7:28.0.0’
implementation ‘com.squareup.okhttp3:logging-interceptor:3.4.1’
package com.bawei.mvplx;
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.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.bawei.mvplx.presenter.LoginPresenter;
import com.bawei.mvplx.utils.Utils;
import com.bawei.mvplx.view.LoginView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener,LoginView {
private Button login;
private EditText pwd;
private EditText phone;
private Button zc;
private LoginPresenter presenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
phone = findViewById(R.id.et_phone);
pwd = findViewById(R.id.et_pwd);
login = findViewById(R.id.bt_logon);
zc = findViewById(R.id.bt_zc);
presenter = new LoginPresenter(this);
login.setOnClickListener(this);
zc.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_logon:
String phone = this.phone.getText().toString();
String pwd = this.pwd.getText().toString();
boolean mobileNO = Utils.isMobileNO(phone);
if (!mobileNO) {
Toast.makeText(this, "手机号不对", Toast.LENGTH_SHORT).show();
return;
}
if (pwd.length() < 6) {
Toast.makeText(this, "密码不能低于六位", Toast.LENGTH_SHORT).show();
return;
}
//传参
presenter.sendParmeter(phone,pwd);
break;
case R.id.bt_zc:
Intent intent = new Intent(MainActivity.this,ZhuceActivity.class);
startActivity(intent);
break;
}
}
@Override
public void view(String status) {
int i = Integer.parseInt(status);
if (i==0000){
Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this,ShowActivity.class);
startActivity(intent);
finish();
return;
}else {
Toast.makeText(this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
}
}
}
//------------------presenter包----------------------
package com.bawei.mvplx.presenter;
import com.bawei.mvplx.MainActivity;
import com.bawei.mvplx.model.LoginModel;
import com.bawei.mvplx.view.LoginView;
/**
-
Created by 叶至成 on 2019/2/15.
*/
public class LoginPresenter {private final LoginModel loginModel;
private final LoginView loginView;public LoginPresenter(LoginView view) {
loginModel = new LoginModel();
loginView = view;
}//传过来的参数
public void sendParmeter(String phone, String pwd) {
loginModel.login(phone,pwd);
//接收从model回调过来的数据给view
loginModel.setLoginLisenter(new LoginModel.OnLoginLisenter() {
@Override
public void onResult(String status) {
loginView.view(status);
}
});
}
}
//---------------------------model包-------------------
package com.bawei.mvplx.model;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
/**
-
Created by 叶至成 on 2019/2/15.
*/
public class LoginModel {
//定义接口
public interface OnLoginLisenter {
void onResult(String status);
}//声明接口
public OnLoginLisenter loginLisenter;//提供一个公共的设置监听的方法
public void setLoginLisenter(OnLoginLisenter loginLisenter) {
this.loginLisenter = loginLisenter;
}private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 0:
String json = (String) msg.obj;
try {
JSONObject jsonObject = new JSONObject(json);
String status = jsonObject.getString(“status”);
Log.d(“xxxxx”,status);
if (loginLisenter!=null){
//回调数据
loginLisenter.onResult(status);
}
} catch (Exception e) {
}
break;
}
}
};public void login(String phone, String pwd) {
//创建OKhttp对象
OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
//创建requestbody并封装请求参数
RequestBody requestBody = new FormBody.Builder()
.add(“phone”, phone)
.add(“pwd”, pwd)
.build();//创建post请求 Request request = new Request.Builder() .url("http://172.17.8.100/small/user/v1/login") .post(requestBody) .build(); Call call = okHttpClient.newCall(request); call.enqueue(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); } });
}
}
//------------------------工具类 utiles----------------
package com.bawei.mvplx.utils;
import android.text.TextUtils;
/**
- Created by 叶至成 on 2019/2/15.
/
public class Utils {
public static boolean isMobileNO(String mobileNums) {
/*
* 判断字符串是否符合手机号码格式
* 移动号段: 134,135,136,137,138,139,147,150,151,152,157,158,159,170,178,182,183,184,187,188
* 联通号段: 130,131,132,145,155,156,170,171,175,176,185,186
* 电信号段: 133,149,153,170,173,177,180,181,189
* @param str
* @return 待检测的字符串
*/
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);
}
}
//----------------------view包
package com.bawei.mvplx.view;
/**
*
*/
public interface LoginView {
void view(String status);
}
//--------------------以上是登录的代码-------------
//--------------------开始注册的代码-------------
package com.bawei.mvplx;
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.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.bawei.mvplx.presenter.ZhucePresenter;
import com.bawei.mvplx.utils.Utils;
import com.bawei.mvplx.view.ZhuceView;
public class ZhuceActivity extends AppCompatActivity implements View.OnClickListener,ZhuceView {
private Button zc;
private EditText pwd;
private EditText phone;
private ZhucePresenter presenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zhuce);
phone = findViewById(R.id.phone);
pwd = findViewById(R.id.pwd);
zc = findViewById(R.id.zc);
presenter = new ZhucePresenter(this);
zc.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String et_phone = phone.getText().toString();
String et_pwd = pwd.getText().toString();
boolean mobileNO = Utils.isMobileNO(et_phone);
if (!mobileNO){
Toast.makeText(this, "手机号不合格", Toast.LENGTH_SHORT).show();
return;
}
if (et_pwd.length()<6){
Toast.makeText(this, "密码不能低于六位", Toast.LENGTH_SHORT).show();
return;
}
presenter.send(et_phone,et_pwd);
}
@Override
public void view(String status) {
Log.i("aaa", "view: "+status);
int i = Integer.parseInt(status);
if (i==0000){
Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(ZhuceActivity.this,MainActivity.class);
startActivity(intent);
finish();
return;
}else {
Toast.makeText(this, "注册失败", Toast.LENGTH_SHORT).show();
}
}
}
------------------presenter包--------------
package com.bawei.mvplx.presenter;
import android.util.Log;
import com.bawei.mvplx.ZhuceActivity;
import com.bawei.mvplx.model.ZhuceModel;
import com.bawei.mvplx.view.ZhuceView;
/**
-
Created by 叶至成 on 2019/2/16.
*/
public class ZhucePresenter {private final ZhuceModel zhuceModel;
private final ZhuceView zhuceView;public ZhucePresenter(ZhuceView view) {
zhuceModel = new ZhuceModel();
zhuceView = view;
}//传过来的参数
public void send(String et_phone, String et_pwd) {
zhuceModel.send(et_phone, et_pwd);
zhuceModel.setZhuceLisenter(new ZhuceModel.OnZhuceLisenter() {
@Override
public void onResult(String status) {
zhuceView.view(status);
}
});
}///接收从model回调的数据给view
@Override
public void onResult(String status) {
Log.i(“lll”, "onResult: "+status);
zhuceView.view(status);
}/
}
//------------------------model包------------------
package com.bawei.mvplx.model;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
/**
-
Created by 叶至成 on 2019/2/16.
*/
public class ZhuceModel {
public interface OnZhuceLisenter{
void onResult(String status);
}
public OnZhuceLisenter zhuceLisenter;public void setZhuceLisenter(OnZhuceLisenter zhuceLisenter) {
this.zhuceLisenter = zhuceLisenter;
}private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case 0:
String json = (String) msg.obj;
try {
JSONObject jsonObject = new JSONObject(json);
String status = jsonObject.getString(“status”);
if (zhuceLisenter!=null){
//回调数据
zhuceLisenter.onResult(status);
}} catch (Exception e) { e.printStackTrace(); } break; } }
};
public void send(String et_phone, String et_pwd) {
//穿件网络请求对象
OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
//请求体
RequestBody requestBody = new FormBody.Builder()
.add(“phone”, et_phone)
.add(“pwd”, et_pwd)
.build();
//创建post请求
Request request = new Request.Builder()
.url(“http://172.17.8.100/small/user/v1/register”)
.post(requestBody)
.build();
Call call = okHttpClient.newCall(request);
call.enqueue(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(); Log.i("asd", "onResponse: " + json); Message message = new Message(); message.what=0; message.obj = json; handler.sendMessage(message); } });
}
}
-----------------------------------view包------------------
package com.bawei.mvplx.view;
/**
*/
public interface ZhuceView {
void view(String status);
}