用到的接口:IView接口和IResponse接口
M:Model层:
public class HttpUtils { private static final String TAG = "HttpUtils"; private static volatile HttpUtils instance; private HttpUtils() { } /** * 双重检验锁机制的单例 * * @return */ public static HttpUtils getInstance() { if (instance == null) { synchronized (HttpUtils.class) { if (null == instance) { instance = new HttpUtils(); } } } return instance; } /** * 网络请求get方式 * * @param url * @param params */ public void get(String url, Map<String, String> params, final IResponse response) { RequestParams requestParams = new RequestParams(url); /** * Map<String, String> map = new HashMap()<> * map.put("mobile", "155567"); * map.put("password","123456"); * </></> */ // map遍历 for (Map.Entry<String, String> entry : params.entrySet()) { requestParams.addQueryStringParameter(entry.getKey(), entry.getValue()); } // 做网络请求 x.http().get(requestParams, 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() { } }); } }
V:View层:
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(); } } }
P:Presenter层:
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; } } }