V层
package com.example.kanghuwei.kanghuwei_exam.ui.activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.kanghuwei.kanghuwei_exam.R;
import com.example.kanghuwei.kanghuwei_exam.beans.LoginBean;
import com.example.kanghuwei.kanghuwei_exam.di.contract.ILoginContract;
import com.example.kanghuwei.kanghuwei_exam.di.presenter.LoginPresenter;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class LoginActivity extends AppCompatActivity implements ILoginContract.LoginView {
@BindView(R.id.et_name)
EditText etName;
@BindView(R.id.et_password)
EditText etPassword;
@BindView(R.id.btn_login)
Button btnLogin;
private LoginPresenter presenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
presenter = new LoginPresenter();
presenter.attachView(this);
}
@Override
public void showData(LoginBean loginBean) {
String status = loginBean.getStatus();
String message = loginBean.getMessage();
if (status.equals("1001")){
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}else if (status.equals("0000")){
Intent intent = new Intent(LoginActivity.this, ShowActivity.class);
startActivity(intent);
finish();
}
}
@OnClick(R.id.btn_login)
public void onViewClicked() {
String name = etName.getText().toString();
String password = etPassword.getText().toString();
if (name.isEmpty()&&password.isEmpty()){
Toast.makeText(this, "账号或者密码不能为空", Toast.LENGTH_SHORT).show();
}else {
presenter.requestData(name,password);
}
}
}
P层
package com.example.kanghuwei.kanghuwei_exam.di.presenter;
import com.example.kanghuwei.kanghuwei_exam.beans.LoginBean;
import com.example.kanghuwei.kanghuwei_exam.di.contract.ILoginContract;
import com.example.kanghuwei.kanghuwei_exam.di.model.LoginModel;
import java.lang.ref.SoftReference;
public class LoginPresenter implements ILoginContract.LoginPresenter {
ILoginContract.LoginView loginview;
private LoginModel model;
private SoftReference<ILoginContract.LoginView> softReference;
@Override
public void attachView(ILoginContract.LoginView loginview) {
this.loginview = loginview;
model = new LoginModel();
softReference = new SoftReference<>(loginview);
}
@Override
public void detachView(ILoginContract.LoginView loginview) {
softReference.clear();
}
@Override
public void requestData(String name, String password) {
model.responseData(name,password,new ILoginContract.LoginModel.CallBack() {
@Override
public void onCallBack(LoginBean loginBean) {
loginview.showData(loginBean);
}
});
}
}
M层
package com.example.kanghuwei.kanghuwei_exam.di.model;
import com.example.kanghuwei.kanghuwei_exam.beans.LoginBean;
import com.example.kanghuwei.kanghuwei_exam.data.Constant;
import com.example.kanghuwei.kanghuwei_exam.di.contract.ILoginContract;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
public class LoginModel implements ILoginContract.LoginModel {
public void responseData(String name, String password, final CallBack callback) {
OkGo.<String>post(Constant.LOGIN_URL+"?phone="+name+"&pwd="+password).execute(new StringCallback() {
@Override
public void onSuccess(Response<String> response) {
String requestData = response.body().toString();
Gson gson = new Gson();
LoginBean loginBean = gson.fromJson(requestData, LoginBean.class);
callback.onCallBack(loginBean);
}
});
}
@Override
public void responseData(CallBack callback) {
}
}
契约类
package com.example.kanghuwei.kanghuwei_exam.di.contract;
import com.example.kanghuwei.kanghuwei_exam.beans.LoginBean;
public interface ILoginContract {
public interface LoginView {
public void showData(LoginBean loginBean);
}
public interface LoginPresenter<LoginView> {
public void attachView(ILoginContract.LoginView loginview);
public void detachView(ILoginContract.LoginView loginview);
public void requestData(String name, String password);
}
public interface LoginModel {
public void responseData(CallBack callback);
public interface CallBack {
public void onCallBack(LoginBean loginBean);
}
}
}
346

被折叠的 条评论
为什么被折叠?



