MVP登录

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);
        }
    }
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值