bean
public class LoginBean { /** * msg : 登录成功 * code : 0 * data : {"age":null,"appkey":"002b28988d38185b","appsecret":"18C758D2F158D9D1F1A585CD1ED45FB6","createtime":"2017-12-09T14:58:18","email":null,"fans":null,"follow":null,"gender":null,"icon":null,"latitude":null,"longitude":null,"mobile":"18735879507","money":null,"nickname":null,"password":"8F669074CAF5513351A2DE5CC22AC04C","praiseNum":null,"token":"95377F37EC59D5E91D64A707E0FB9CCE","uid":1570,"userId":null,"username":"18735879507"} */ private String msg; private String code; private DataBean data; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public DataBean getData() { return data; } public void setData(DataBean data) { this.data = data; } public static class DataBean { /** * age : null * appkey : 002b28988d38185b * appsecret : 18C758D2F158D9D1F1A585CD1ED45FB6 * createtime : 2017-12-09T14:58:18 * email : null * fans : null * follow : null * gender : null * icon : null * latitude : null * longitude : null * mobile : 18735879507 * money : null * nickname : null * password : 8F669074CAF5513351A2DE5CC22AC04C * praiseNum : null * token : 95377F37EC59D5E91D64A707E0FB9CCE * uid : 1570 * userId : null * username : 18735879507 */ private Object age; private String appkey; private String appsecret; private String createtime; private Object email; private Object fans; private Object follow; private Object gender; private Object icon; private Object latitude; private Object longitude; private String mobile; private Object money; private Object nickname; private String password; private Object praiseNum; private String token; private int uid; private Object userId; private String username; public Object getAge() { return age; } public void setAge(Object age) { this.age = age; } public String getAppkey() { return appkey; } public void setAppkey(String appkey) { this.appkey = appkey; } public String getAppsecret() { return appsecret; } public void setAppsecret(String appsecret) { this.appsecret = appsecret; } public String getCreatetime() { return createtime; } public void setCreatetime(String createtime) { this.createtime = createtime; } public Object getEmail() { return email; } public void setEmail(Object email) { this.email = email; } public Object getFans() { return fans; } public void setFans(Object fans) { this.fans = fans; } public Object getFollow() { return follow; } public void setFollow(Object follow) { this.follow = follow; } public Object getGender() { return gender; } public void setGender(Object gender) { this.gender = gender; } public Object getIcon() { return icon; } public void setIcon(Object icon) { this.icon = icon; } public Object getLatitude() { return latitude; } public void setLatitude(Object latitude) { this.latitude = latitude; } public Object getLongitude() { return longitude; } public void setLongitude(Object longitude) { this.longitude = longitude; } public String getMobile() { return mobile; } public void setMobile(String mobile) { this.mobile = mobile; } public Object getMoney() { return money; } public void setMoney(Object money) { this.money = money; } public Object getNickname() { return nickname; } public void setNickname(Object nickname) { this.nickname = nickname; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Object getPraiseNum() { return praiseNum; } public void setPraiseNum(Object praiseNum) { this.praiseNum = praiseNum; } public String getToken() { return token; } public void setToken(String token) { this.token = token; } public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } public Object getUserId() { return userId; } public void setUserId(Object userId) { this.userId = userId; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } } }
登录
<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" android:orientation="vertical" tools:context="com.bwie.lian.login.LoginActivity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp"> <ImageView android:id="@+id/login_back" android:layout_width="25dp" android:layout_height="25dp" android:layout_centerVertical="true" android:layout_marginLeft="15dp" android:background="@drawable/cha" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:textColor="#ff3660" android:text="登录" android:textSize="25sp" /> </RelativeLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="5dp" android:text="账号" /> <EditText android:id="@+id/et_phone" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="手机号" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:layout_marginTop="10dp" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="5dp" android:text="密码" /> <EditText android:id="@+id/et_pwd" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:hint="请输入密码" /> <ImageView android:id="@+id/img_pwd" android:layout_width="25dp" android:layout_height="15dp" android:layout_gravity="center_vertical" android:background="@drawable/yannormal" /> </LinearLayout> <Button android:id="@+id/btn_login" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="15dp" android:text="登录" /> <TextView android:id="@+id/tv_register" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:text="请注册" /> </LinearLayout>
ILoginModel
public interface ILoginModel { public void getLogin(String mobile, String password, OnNetListener<LoginBean> onNetListener); }
LoginModel
public class LoginModel implements ILoginModel{ @Override public void getLogin(String mobile, String password, final OnNetListener<LoginBean> onNetListener) { ServiceApi serviceApi = RetrofitHelper.getServiceApi(); serviceApi.getLogin(mobile,password) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Subscriber<LoginBean>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(LoginBean loginBean) { onNetListener.onSuccess(loginBean); } }); } }
ILoginView
public interface ILoginView { public String getAccount(); public String getpwd(); public void showLogin(LoginBean loginBean, String code, String msg); }
LoginPresenter
public class LoginPresenter { private final ILoginModel iLoginModel; private final ILoginView iLoginView; public LoginPresenter(ILoginView iLoginView) { this.iLoginView = iLoginView; iLoginModel = new LoginModel(); } public void getLogin() { String account = iLoginView.getAccount(); String pwd = iLoginView.getpwd(); //去调用model,进行登陆 iLoginModel.getLogin(account, pwd, new OnNetListener<LoginBean>() { @Override public void onSuccess(LoginBean loginBean) { //保存登陆成功后的数据,可以保存到SP,也可以保存到数据库 String code = loginBean.getCode(); String msg = loginBean.getMsg(); iLoginView.showLogin(loginBean, code, msg); } @Override public void onFailure(Exception e) { } }); } }
LoginActivity
public class LoginActivity extends AppCompatActivity implements View.OnClickListener, ILoginView { private ImageView mLoginBack; private EditText mEtPhone; private EditText mEtPwd; private Button mBtnLogin; private TextView mTvRegister; private LoginPresenter loginPresenter; String codes; String msgs; private static final String TAG = "MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); //关联presenter loginPresenter = new LoginPresenter(this); initView(); } private void initView() { mLoginBack = (ImageView) findViewById(R.id.login_back); mEtPhone = (EditText) findViewById(R.id.et_phone); mEtPwd = (EditText) findViewById(R.id.et_pwd); mBtnLogin = (Button) findViewById(R.id.btn_login); mBtnLogin.setOnClickListener(this); mTvRegister = (TextView) findViewById(R.id.tv_register); mTvRegister.setOnClickListener(this); mLoginBack.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { default: break; case R.id.login_back: //返回 this.finish(); break; case R.id.btn_login: //登录 loginPresenter.getLogin(); Intent intent1 = new Intent(LoginActivity.this, MainActivity.class); intent1.putExtra("flag", 5); startActivity(intent1); break; case R.id.tv_register: //注册 Intent intent = new Intent(LoginActivity.this, RegisterActivity.class); startActivity(intent); break; } } @Override public String getAccount() { return mEtPhone.getText().toString().trim(); } @Override public String getpwd() { return mEtPwd.getText().toString().trim(); } @Override public void showLogin(LoginBean loginBean, String code, String msg) { codes = code; msgs = msg; if (codes.equals("0")) { Toast.makeText(this, msgs, Toast.LENGTH_SHORT).show(); LoginBean.DataBean data = loginBean.getData(); int uid = data.getUid(); String name = data.getUsername(); String token = data.getToken(); MyApp.editor.putInt("uid", uid); MyApp.editor.putString("name", name); MyApp.editor.putString("token", token); MyApp.editor.putBoolean("flag", true); MyApp.editor.commit(); finish(); } else { MyApp.editor.putBoolean("flag", false); MyApp.editor.commit(); Toast.makeText(this, msgs, Toast.LENGTH_SHORT).show(); } } }
注册
bean
public class RegisterBean { private String msg; private String code; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } }
<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" android:orientation="vertical" tools:context="com.bwie.lian.register.RegisterActivity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp"> <ImageView android:id="@+id/register_back" android:layout_width="25dp" android:layout_height="25dp" android:layout_centerVertical="true" android:background="@drawable/leftjiantou" android:layout_marginLeft="15dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:textSize="25sp" android:textColor="#ff3660" android:text="注册"/> </RelativeLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="账号" android:layout_marginRight="5dp"/> <EditText android:id="@+id/et_phone" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="手机号"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:orientation="horizontal" android:layout_marginTop="10dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密码" android:layout_marginRight="5dp"/> <EditText android:id="@+id/et_pwd" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:hint="请输入密码"/> <ImageView android:id="@+id/img_pwd" android:layout_width="25dp" android:layout_height="15dp" android:background="@drawable/yannormal" android:layout_gravity="center_vertical"/> </LinearLayout> <Button android:id="@+id/btn_register" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="注册" android:layout_margin="15dp"/> </LinearLayout>
IRegisterModel
public interface IRegisterModel { public void register(String account, String pwd, OnNetListener<RegisterBean> onNetListener);
}
RegisterModel
public class RegisterModel implements IRegisterModel{ @Override public void register(String account, String pwd, final OnNetListener<RegisterBean> onNetListener) { ServiceApi serviceApi = RetrofitHelper.getServiceApi(); serviceApi.getRegister(account, pwd) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Subscriber<RegisterBean>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { e.getMessage(); } @Override public void onNext(RegisterBean registerBean) { onNetListener.onSuccess(registerBean); } }); } }
IRegisterView
public interface IRegisterView { public String getAccount(); public String getPwd(); public void showRegister(String code, String msg); }RegisterPresenter
public class RegisterPrersenter { private final IRegisterModel iRegisterModel; private final IRegisterView iRegisterView; public RegisterPrersenter(IRegisterView iRegisterView) { this.iRegisterView = iRegisterView; iRegisterModel = new RegisterModel(); } public void register() { String account = iRegisterView.getAccount(); String pwd = iRegisterView.getPwd(); iRegisterModel.register(account, pwd, new OnNetListener<RegisterBean>() { @Override public void onSuccess(RegisterBean registerBean) { String code = registerBean.getCode(); String msg = registerBean.getMsg(); iRegisterView.showRegister(code, msg); } @Override public void onFailure(Exception e) { } }); } }RegisterActivity
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener, IRegisterView { private ImageView mRegisterBack; private EditText mEtPhone; private EditText mEtPwd; private Button mBtnRegister; private RegisterPrersenter registerPrersenter; String codes; String msgs; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register); registerPrersenter = new RegisterPrersenter(this); initView(); } private void initView() { mRegisterBack = (ImageView) findViewById(R.id.register_back); mRegisterBack.setOnClickListener(this); mEtPhone = (EditText) findViewById(R.id.et_phone); mEtPwd = (EditText) findViewById(R.id.et_pwd); mBtnRegister = (Button) findViewById(R.id.btn_register); mBtnRegister.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { default: break; case R.id.register_back: //返回 this.finish(); break; case R.id.btn_register: //注册 registerPrersenter.register(); break; } } @Override public String getAccount() { return mEtPhone.getText().toString().trim(); } @Override public String getPwd() { return mEtPwd.getText().toString().trim(); } @Override public void showRegister(String code, String msg) { codes = code; msgs = msg; if (codes.equals("0")) { Toast.makeText(this, msgs, Toast.LENGTH_SHORT).show(); if (msgs.equals("注册成功")) { Intent intent = new Intent(this, LoginActivity.class); startActivity(intent); } } else { Toast.makeText(this, msgs, Toast.LENGTH_SHORT).show(); } } }