1.activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/txt_show" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="hello world" /> <EditText android:id="@+id/et_username" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:hint="请输入用户名" /> <EditText android:id="@+id/et_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入密码" /> <Button android:id="@+id/btn_login" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="登录" /> </LinearLayout>
2.MainActivity
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();
}
}
}
3.IResponse
package com.bwie.loginmvpdemo;
/**
* Created by WuXirui
* Create Time: 2017/11/3
* Description:
*/
public interface IResponse {
void onSuccess(String data);
void onFailed(String message);
}
4.IView
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);
}
5.BaseApplication
package com.bwie.loginmvpdemo;
import android.app.Application;
import org.xutils.x;
/**
* Created by WuXirui
* Create Time: 2017/11/3
* Description:
*/
public class BaseApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
x.Ext.init(this);
x.Ext.setDebug(BuildConfig.DEBUG);
}
}
6.LoginPresenter
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;
}
}
}
7.HttpUtils
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() {
}
});
}
}