Model层:
登录:
package com.baway.a1509amvp.view.model;
import android.content.Context;
import com.baway.a1509amvp.view.bean.LoginBean;
import com.baway.a1509amvp.view.net.OnNetListener;
import java.util.Map;
/**
* Created by peng on 2017/11/3.
*/
public interface ILoginModel {
//GET请求
public void login(Context context, String url, OnNetListener onNetListener);
//POST请求
public void loginPost(Context context, String url, Map
}
package com.baway.a1509amvp.view.model;
import android.content.Context;
import com.baway.a1509amvp.view.bean.LoginBean;
import com.baway.a1509amvp.view.net.HttpUtils;
import com.baway.a1509amvp.view.net.OnNetListener;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.Map;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
/**
* Created by peng on 2017/11/3.
*/+
public class LoginModel implements ILoginModel {
@Override
public void login(Context context, String url, final OnNetListener onNetListener) {
HttpUtils.getHttpUtils(context).doGet(url, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
onNetListener.onFailure(e);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
onNetListener.onSuccess(response.body().string());
}
});
}
@Override
public void loginPost(Context context, String url, Map
HttpUtils.getHttpUtils(context).doPost(url, params, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
onNetListener.onFailure(e);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
LoginBean loginBean = new Gson().fromJson(response.body().string(), LoginBean.class);
onNetListener.onSuccess(loginBean);
}
});
}
}
注册:
package com.baway.a1509amvp.view.model;
import android.content.Context;
import com.baway.a1509amvp.view.net.OnNetListener;
/**
* Created by peng on 2017/11/3.
*/
public interface IRegisterModel {
public void register(Context context, String url, OnNetListener onNetListener);
}
package com.baway.a1509amvp.view.model;
import android.content.Context;
import com.baway.a1509amvp.view.net.HttpUtils;
import com.baway.a1509amvp.view.net.OnNetListener;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
/**
* Created by peng on 2017/11/3.
*/
public class RegisterModel implements IRegisterModel {
@Override
public void register(Context context, String url, final OnNetListener onNetListener) {
HttpUtils.getHttpUtils(context).doGet(url, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
onNetListener.onFailure(e);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
onNetListener.onSuccess(response.body().string());
}
});
}
}
presenter层:
登录逻辑:
package com.baway.a1509amvp.view.presenter;
import android.content.Context;
import com.baway.a1509amvp.view.bean.LoginBean;
import com.baway.a1509amvp.view.model.ILoginModel;
import com.baway.a1509amvp.view.model.LoginModel;
import com.baway.a1509amvp.view.net.OnNetListener;
import com.baway.a1509amvp.view.view.IMainActivity;
/**
* Created by peng on 2017/11/3.
*/
public class LoginPresenter {
private final ILoginModel loginModel;
private IMainActivity iMainActivity;
private Context context;
public LoginPresenter(IMainActivity iMainActivity) {
//创建Model层对象
context = (Context) iMainActivity;
loginModel = new LoginModel();
this.iMainActivity = iMainActivity;
}
/**
* 登陆
* http://120.27.23.105/user/login?mobile=15612346598&password=123456
*/
public void login() {
String urlStr = "http://120.27.23.105/user/login?mobile=%s&password=%s";
String phone = iMainActivity.getPhone();
String pwd = iMainActivity.getPwd();
String url = String.format(urlStr, phone, pwd);
loginModel.login(context,url, new OnNetListener
@Override
public void onSuccess(String str) {
// iMainActivity.show(str);
iMainActivity.toSecond();
}
@Override
public void onSuccess(LoginBean loginBean) {
}
@Override
public void onFailure(Exception e) {
}
});
}
public void loginPost() {
}
}
注册逻辑:
package com.baway.a1509amvp.view.presenter;
import android.content.Context;
import com.baway.a1509amvp.view.bean.LoginBean;
import com.baway.a1509amvp.view.model.IRegisterModel;
import com.baway.a1509amvp.view.model.RegisterModel;
import com.baway.a1509amvp.view.net.OnNetListener;
import com.baway.a1509amvp.view.view.IMainActivity;
/**
* Created by peng on 2017/11/3.
*/
public class RegisterPresenter {
private IMainActivity iMainActivity;
private IRegisterModel iRegisterModel;
private Context context;
public RegisterPresenter(IMainActivity iMainActivity) {
context = (Context) iMainActivity;
this.iMainActivity = iMainActivity;
iRegisterModel = new RegisterModel();
}
public void register() {
String urlStr = "http://120.27.23.105/user/reg?mobile=%s&password=%s";
String phone = iMainActivity.getPhone();
String pwd = iMainActivity.getPwd();
String url = String.format(urlStr, phone, pwd);
iRegisterModel.register(context,url, new OnNetListener
@Override
public void onSuccess(String str) {
iMainActivity.show(str);
}
@Override
public void onSuccess(LoginBean loginBean) {
}
@Override
public void onFailure(Exception e) {
}
});
}
}
登陆成功逻辑:
package com.baway.a1509amvp.view.presenter;
import android.content.Context;
import com.baway.a1509amvp.view.bean.LoginBean;
import com.baway.a1509amvp.view.model.ILoginModel;
import com.baway.a1509amvp.view.model.IRegisterModel;
import com.baway.a1509amvp.view.model.LoginModel;
import com.baway.a1509amvp.view.model.RegisterModel;
import com.baway.a1509amvp.view.net.Api;
import com.baway.a1509amvp.view.net.OnNetListener;
import com.baway.a1509amvp.view.view.IMainActivity;
import java.util.HashMap;
import java.util.Map;
/**
* Created by peng on 2017/11/3.
*/
public class MainPresenter {
private ILoginModel iLoginModel;
private IRegisterModel iRegisterModel;
private IMainActivity iMainActivity;
private Context context;
public MainPresenter(IMainActivity iMainActivity) {
context = (Context) iMainActivity;
this.iMainActivity = iMainActivity;
iLoginModel = new LoginModel();
iRegisterModel = new RegisterModel();
}
//用post请求进行登陆
public void login() {
// String url = "http://120.27.23.105/user/login";
Map
params.put("mobile", iMainActivity.getPhone());
params.put("password", iMainActivity.getPwd());
iLoginModel.loginPost(context, Api.LOGIN, params, new OnNetListener
@Override
public void onSuccess(String str) {
}
@Override
public void onSuccess(LoginBean loginBean) {
iMainActivity.show(loginBean.getMsg());
}
@Override
public void onFailure(Exception e) {
}
});
}
public void register() {
}
}
view层:
获取账号密码类:
package com.baway.a1509amvp.view.view;
/**
* Created by peng on 2017/11/3.
*/
public interface IMainActivity {
//获取手机号
public String getPhone();
//获取密码
public String getPwd();
//显示登陆成功后的数据
public void show(String str);
//跳转
public void toSecond();
}
MainActivity主类:
package com.baway.a1509amvp.view.view;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.baway.a1509amvp.R;
import com.baway.a1509amvp.view.presenter.LoginPresenter;
import com.baway.a1509amvp.view.presenter.MainPresenter;
import com.baway.a1509amvp.view.presenter.RegisterPresenter;
public class MainActivity extends AppCompatActivity implements View.OnClickListener, IMainActivity {
/**
* 请输入账号
*/
private EditText mEtAccount;
/**
* 请输入密码
*/
private EditText mEtPwd;
/**
* 登陆
*/
private Button mBtLogin;
private LoginPresenter loginPresenter;
/**
* 注册
*/
private Button mBtRegister;
private RegisterPresenter registerPresenter;
private MainPresenter mainPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loginPresenter = new LoginPresenter(this);
registerPresenter = new RegisterPresenter(this);
mainPresenter = new MainPresenter(this);
initView();
}
private void initView() {
mEtAccount = (EditText) findViewById(R.id.et_account);
mEtPwd = (EditText) findViewById(R.id.et_pwd);
mBtLogin = (Button) findViewById(R.id.bt_login);
mBtLogin.setOnClickListener(this);
mBtRegister = (Button) findViewById(R.id.bt_register);
mBtRegister.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
default:
break;
case R.id.bt_login:
// loginPresenter.login();
mainPresenter.login();
break;
case R.id.bt_register:
registerPresenter.register();
break;
}
}
@Override
public String getPhone() {
return mEtAccount.getText().toString().trim();
}
@Override
public String getPwd() {
return mEtPwd.getText().toString().trim();
}
@Override
public void show(final String str) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void toSecond() {
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
}
}