MVP

来源:互联网 发布:驱动精灵 for linux 编辑:程序博客网 时间:2024/06/05 16:27
package com.bwie.loginmvpdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener,IView {
    private static final String TAG = "MainActivity";

    private EditText etUsername;
    private EditText etPassword;
    private Button btnLogin;
    private TextView txtShow;

    private  LoginPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        etUsername = (EditText) findViewById(R.id.et_username);
        etPassword = (EditText) findViewById(R.id.et_password);
        btnLogin = (Button) findViewById(R.id.btn_login);
        txtShow = (TextView) findViewById(R.id.txt_show);

        btnLogin.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_login:
                String username = etUsername.getText().toString().trim();
                String password  =etPassword.getText().toString().trim();

                // 创建P的对象
                presenter = new LoginPresenter();
                presenter.attachView(this);

//                LoginPresenter presenter1 = new LoginPresenter(this);
                // 并且调用P层的方法
                presenter.login(username, password);
                break;
        }
    }

    @Override
    public void success(String data) {
        // 接收到P层传过来的数据之后做修改View的操作
        txtShow.setText("登录成功");
    }

    @Override
    public void failed(String message) {
        txtShow.setText("登录失败");
    }


    // V层销毁之后调用P层提供的解绑方法
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.detatch();
        }
    }

}


package com.bwie.loginmvpdemo;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by WuXirui
 * Create Time: 2017/11/3
 * Description:
 */

// 创建P层的类
public class LoginPresenter {

    private IView iView;

    // 提供初始化IView对象的一个方法

    public LoginPresenter() {

    }

    public LoginPresenter(IView iView) {
        this.iView = iView;
    }


    public void attachView(IView iView) {
        this.iView = iView;
    }


    /**
     * 登录
     * @param username
     * @param password
     */

    // 调用Model层的请求网络或数据库的方法
    public void login(String username, String password) {
        Map<String, String> map = new HashMap<>();
        map.put("mobile", username);
        map.put("password", password);
        HttpUtils.getInstance().get("http://120.27.23.105/user/login", map,
                new IResponse() {
                    @Override
                    public void onSuccess(String data) {
                        // 把M层拿到的数据回调给V层
                        iView.success(data);
                    }

                    @Override
                    public void onFailed(String message) {
                        iView.failed(message);
                    }
                });
    }

    /**
     * 提供解绑的方法,避免内存泄漏
     */
    public void detatch(){
        if (iView != null) {
            iView = null;
        }
    }
}


package com.bwie.loginmvpdemo;

/**
 * Created by WuXirui
 * Create Time: 2017/11/3
 * Description:
 */

// 第一步,定义一个View层的回调接口
public interface IView {
    void success(String data);
    void failed(String message);
}



package com.bwie.loginmvpdemo;

import android.util.Log;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.Map;

/**
 * Created by WuXirui
 * Create Time: 2017/11/3
 * Description:
 */

public class HttpUtils {
    private static final String TAG = "HttpUtils";
    private static volatile HttpUtils instance;

    private HttpUtils() {

    }

    public static HttpUtils getInstance() {
        if (null == instance) {
            synchronized (HttpUtils.class) {
                if (instance == null) {
                    instance = new HttpUtils();
                }
            }
        }
        return instance;
    }


    public void get(String url, Map<String, String> map, final IResponse response) {
        RequestParams params = new RequestParams(url);
        for (Map.Entry<String, String> entry : map.entrySet()) {
            params.addQueryStringParameter(entry.getKey(), entry.getValue());
        }

        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.i(TAG, "onSuccess: " + result);
                response.onSuccess(result);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.e(TAG, "onError: " + ex.getMessage());
                response.onFailed(ex.getMessage());
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}


package com.bwie.loginmvpdemo;

/**
 * Created by WuXirui
 * Create Time: 2017/11/3
 * Description:
 */

public interface IResponse {
    void onSuccess(String data);
    void onFailed(String message);
}








原创粉丝点击