第一步 分包
Contract_interface.java
//总的Interface
public interface Contract_Interface {
//登录--V层接口
public interface Login_View_Interface{
//数据刷新
public void showData(String message);
}
//登录--P层接口
public interface Login_Presenter_Interface<Login_View_Interface>{
//绑定
public void attahView(Login_View_Interface login_view_interface);
//解绑
public void deathView(Login_View_Interface login_view_interface);
//吧账号密码交给M层
public void requestData(String phone, String pass);
}
//登录--M层接口
public interface Login_Model_Interface{
//网络请求数据
public void getJSon(String phone, String pass, CallBack callBack);
//接口回调
public interface CallBack{
//将结果返回给P层
public void responseData(String message);
}
}
}
–V层
public class MainActivity extends AppCompatActivity implements Contract_Interface.Login_View_Interface {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_login );
//查找控件
tv_kszc = (TextView)findViewById( R.id.tv_kszc );
btn_login = (Button)findViewById( R.id.btn_login );
et_login_phone = (EditText)findViewById( R.id.et_login_phone );
et_login_pass = (EditText)findViewById( R.id.et_login_pass );
img_login_eye = (ImageView)findViewById( R.id.img_login_eye );
//是否记住密码SP
sp = getSharedPreferences( "user", MODE_PRIVATE );
if (sp.getBoolean("jizhu", false)) {
ck_jzmm.setChecked( sp.getBoolean("jizhu", false));
et_login_phone.setText( sp.getString("name", ""));
et_login_pwd.setText( sp.getString("pass", ""));
}
//创建P层
presenter_login = new Presenter_Login();
//绑定
presenter_login.attahView( this );
//当我点击立即登录按钮时
login();
}
//当我点击立即登录按钮时
private void login() {
btn_login.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
//得到文本框的内容
String phone = et_login_phone.getText().toString();
String pass = et_login_pass.getText().toString();
//调用p层的方法
presenter_login.requestData(phone,pass);
}
} );
}//当我点击立即登录按钮时
@Override
//数据刷新
public void showData(final String message) {
runOnUiThread( new Runnable() {
@Override
public void run() {
//Toast.makeText( MainActivity.this, ""+message, Toast.LENGTH_SHORT ).show();
//解析数据--message
JSONObject json = null;
try {
json = new JSONObject( message );
String status1 = json.getString( "message" );
String status = json.getString( "status" );
//判断
if (status.equals( "0000" )) {
//跳转到首页
Intent intent = new Intent( MainActivity.this, ShouYeActivity.class );
startActivity( intent );
//判断是否记住密码
SharedPreferences.Editor editor = sp.edit();
editor.putBoolean( "jizhu",ck_jzmm.isChecked() );
editor.putString("name", et_login_phone.getText().toString().trim());
editor.putString("pass", et_login_pwd.getText().toString().trim());
editor.commit();
finish();
Toast.makeText( MainActivity.this, ""+status1, Toast.LENGTH_SHORT ).show();
}else{
Toast.makeText( MainActivity.this, ""+status1, Toast.LENGTH_SHORT ).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
} );//线程
}
//销毁时
@Override
protected void onDestroy() {
super.onDestroy();
presenter.deathView( this );
}
}
–P层
public class Presenter_Login implements Contract_Interface.Login_Presenter_Interface<Contract_Interface.Login_View_Interface> {
Contract_Interface.Login_View_Interface login_view_interface;
private Contract_Interface.Login_Model_Interface model_login;
@Override
//绑定
public void attahView(Contract_Interface.Login_View_Interface login_view_interface) {
this.login_view_interface=login_view_interface;
//创建M层
model_login = new Model_Login();
//新建软引用
softReference = new SoftReference<>( model );
}
@Override
//解绑
public void deathView(Contract_Interface.Login_View_Interface login_view_interface) {
//清理内存
softReference.clear();
}
@Override
//账号密码发送给M层
public void requestData(String phone, String pass) {
//调用M层的网络请求
model_login.getJSon( phone,pass,new Contract_Interface.Login_Model_Interface.CallBack() {
@Override
//数据返回给V层
public void responseData(String message) {
login_view_interface.showData(message);
}
} );
}
}
–M层
public class Model_Login implements Contract_Interface.Login_Model_Interface {
//网络请求数据
@Override
public void getJSon(String phone, String pass, CallBack callBack) {
//OKHttp的异步请求
okHttp(phone,pass,callBack);
}
//OKHttp的异步请求
private void okHttp(String phone, String pass, final CallBack callBack){
//创建OKHttp的对象
OkHttpClient build = new OkHttpClient.Builder().build();
//空表头
FormBody formBody = new FormBody.Builder().build();
Request request = new Request.Builder()
.method( "POST",formBody)
.url( Constant.LOGIN_URL+"?phone="+phone+"&pwd="+pass)
.build();
Call call = build.newCall( request );
call.enqueue( new Callback() {
@Override
public void onFailure(Call call, IOException e) {
//登录失败时
String message = e.getMessage();
callBack.responseData(message);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
//登录成功时
String message = response.body().string();
callBack.responseData( message );
}
} );
}
}
–Constant类
public class Constant {
//登录的接口
public static final String LOGIN_URL = "http://172.17.8.100/small/user/v1/login";
//注册的接口
public static final String REGISTER_URL = "http://172.17.8.100/small/user/v1/register";
}