Activity页面
public class Main2Activity extends AppCompatActivity implements LogContract.ILogview {
private LogPresenter logPresenter;
private EditText phone;
private EditText pwd;
private Button login;
private String logUrl="http://172.17.8.100/small/user/v1/login";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
logPresenter = new LogPresenter();
logPresenter.attch(this);
//初始化控件
initView();
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String phone1 = phone.getText().toString();
String pwd1 = pwd.getText().toString();
logPresenter.logShow(phone1,pwd1,logUrl);
}
});
}
private void initView() {
phone = findViewById(R.id.login_name);
pwd = findViewById(R.id.login_pwd);
login = findViewById(R.id.login_bt);
}
@Override
public void getLogRequest(String data) {
Gson gson = new Gson();
LoginBean loginBean = gson.fromJson(data, LoginBean.class);
String status = loginBean.getStatus();
if (status.equals("0000")){
Intent intent = new Intent(Main2Activity.this,MainActivity.class);
startActivity(intent);
}else{
String message = loginBean.getMessage();
Toast.makeText(this,message,Toast.LENGTH_LONG).show();
}
}
}
//Mvp框架
public interface LogContract {
//Iview
interface ILogview{
void getLogRequest(String data);
}
//Imodel
interface ILogModel{
void getLogShow(String phone,String pwd,String url,CallBack lcallBack);
public interface CallBack{
void onSuccess(String list);
void onFail();
}
}
//Ipresenter
interface ILogpresenter{
void attch(ILogview Iview);
void detch();
void logShow(String phone,String pwd,String url);
}
}
//m层
public class LogModel implements LogContract.ILogModel {
@Override
public void getLogShow(String phone, String pwd, String url, final CallBack lcallBack) {
HashMap<String, String> map = new HashMap<>();
map.put("phone",phone);
map.put("pwd",pwd);
HttpVolley.getInstance().httpVolleyPost(url, map, new HttpVolley.VolleyCallBack() {
@Override
public void onSuccess(String data) {
lcallBack.onSuccess(data);
}
@Override
public void onFail(VolleyError error) {
lcallBack.onFail();
}
});
}
}
//P层
public class LogPresenter implements LogContract.ILogpresenter {
private LogModel logModel;
private LogContract.ILogview iview;
@Override
public void attch(LogContract.ILogview Iview) {
logModel = new LogModel();
this.iview=Iview;
}
@Override
public void detch() {
if (logModel!=null){
logModel=null;
}
if (iview!=null){
iview=null;
}
System.gc();
}
@Override
public void logShow(String phone, String pwd, String url) {
logModel.getLogShow(phone, pwd, url, new LogContract.ILogModel.CallBack() {
@Override
public void onSuccess(String list) {
iview.getLogRequest(list);
}
@Override
public void onFail() {
}
});
}
}