MVP的搭建以及登录

//首先导入okhttp的依赖以及Gson解析的依赖
//在布局文件中写入

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.activity.LoginActivity"
    android:orientation="vertical"
    >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登录"
        android:textSize="30sp"
        android:gravity="center"
        />
    <EditText
        android:id="@+id/ed_userName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入用户名"
        />
    <EditText
        android:id="@+id/ed_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入密码"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <Button
            android:id="@+id/button_login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="登录"
            />
    </LinearLayout>

</LinearLayout>

//写一个mvp的实现接口封装

public interface LoginContract {
    //v层接口
    public interface ILoginView{
        //数据显示
        public void showData(String responseData);
    }
    //p层接口
    public interface ILoginPresenter<ILoginView>{
        //绑定
        public void attachView(ILoginView loginView);

        //解绑
        public void detachView(ILoginView loginView);

        //数据请求  请求m层数据  做登录处理
        public void requestLoginData(String userName,String pwd);
    }
    //M层接口
    public interface ILoginModel{
        //做登录的接口请求
        public void containLoginData(String userName, String pwd, CallBack callback);

        //接口回调
        public interface CallBack{
            public void responseData(String responseData);
        }
    }
}

//主页面继承V层实现接口

public class LoginActivity extends AppCompatActivity implements LoginContract.ILoginView {

    @butterknife.BindView(R.id.ed_userName)
    EditText edUserName;
    @butterknife.BindView(R.id.ed_password)
    EditText edPassword;
    @butterknife.BindView(R.id.button_login)
    Button buttonLogin;

    private LoginContract.ILoginPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        butterknife.ButterKnife.bind(this);
        //创建P层
        presenter = new LoginPresenterImpl();
        presenter.attachView(this);
    }

    @butterknife.OnClick(R.id.button_login)
    public void onViewClicked() {
        String userName = edUserName.getText().toString();
        String pwd = edPassword.getText().toString();
        presenter.requestLoginData(userName,pwd);

    }

    @Override
    public void showData(final String responseData) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {

             Toast.makeText(LoginActivity.this,responseData,Toast.LENGTH_SHORT).show();

            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //解绑
        presenter.detachView(this);
    }
}

//p层实现继承P层接口

public class LoginPresenterImpl implements LoginContract.ILoginPresenter<LoginContract.ILoginView> {
    LoginContract.ILoginView loginView;
    private SoftReference<LoginContract.ILoginView> reference;
    private LoginContract.ILoginModel loginModel;


    @Override
    public void attachView(LoginContract.ILoginView iLoginView) {
        this.loginView=iLoginView;
        //软引用
        reference = new SoftReference<LoginContract.ILoginView>(loginView);
      //创建M层
        loginModel = new LoginModelImpl();

    }

    @Override
    public void detachView(LoginContract.ILoginView iLoginView) {
        reference.clear();
    }

    @Override
    public void requestLoginData(String userName, String pwd) {
        loginModel.containLoginData(userName, pwd, new LoginContract.ILoginModel.CallBack() {
            @Override
            public void responseData(String responseData) {
                loginView.showData(responseData);
            }
        });
    }
}

//M层

public class LoginModelImpl implements LoginContract.ILoginModel {
    @Override
    public void containLoginData(final String userName, final String pwd, CallBack callback) {
        //requestLoginDataEnqueue(userName,pwd, callback);
        requestLoginDataByUtil(userName,pwd,callback);
    }
//okhttp的封装
    private void requestLoginDataByUtil(String userName, String pwd, final CallBack callback) {
        FormBody formBody = new FormBody.Builder().build();
        String urlString=Constant.LOGIN_URL+"?phone="+userName+"&pwd="+pwd;
        OkhttpUtils.getInstance().post(formBody, urlString, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                callback.responseData(e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String responseData = response.body().string();
                callback.responseData(responseData);
            }
        });
    }
//okhttp
    private void requestLoginDataEnqueue(String userName, String pwd, final CallBack callback){
        OkHttpClient client = new OkHttpClient.Builder().build();
        FormBody formBody = new FormBody.Builder().build();
        Request request=new Request.Builder()
                .method("POST",formBody)
                .url(Constant.LOGIN_URL+"?phone="+userName+"&pwd="+pwd)
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                String responseData = e.getMessage();
                callback.responseData(responseData);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String responseData = response.body().string();
                callback.responseData(responseData);
            }
        });
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值