//你的bean包准备好了吗?
//现在要创建的是Login的bean类
public class LoginBean {
/**
* msg : 登录成功
* code : 0
* data : {"age":null,"appkey":"069bea6b57162540","appsecret":"6021EFF0471F50DD135AD0FD1CBFF497","createtime":"2017-11-14T10:11:11","email":null,"gender":null,"icon":null,"mobile":"18810425289","money":null,"nickname":null,"password":"8F669074CAF5513351A2DE5CC22AC04C","token":"B8D91FE59FCF7B0525313AFD4470E326","uid":2276,"username":"18810425289"}
*/
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 : 069bea6b57162540
* appsecret : 6021EFF0471F50DD135AD0FD1CBFF497
* createtime : 2017-11-14T10:11:11
* email : null
* gender : null
* icon : null
* mobile : 18810425289
* money : null
* nickname : null
* password : 8F669074CAF5513351A2DE5CC22AC04C
* token : B8D91FE59FCF7B0525313AFD4470E326
* uid : 2276
* username : 18810425289
*/
private Object age;
private String appkey;
private String appsecret;
private String createtime;
private Object email;
private Object gender;
private Object icon;
private String mobile;
private Object money;
private Object nickname;
private String password;
private String token;
private int uid;
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 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 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 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 String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
}
//注册的bean类也要准备好呦
public class RegBean {
/**
* msg : 注册成功
* code : 0
*/
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;
}
}
//model的包里放了什么呢??
//哈哈哈哈放了一个LoginModel的类
import com.google.gson.Gson;
import java.io.IOException;
import baway.com.denglvzhuce.bean.LoginBean;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class LoginModel {
private String path="https://www.zhaoapi.cn/user/login";
//https://www.zhaoapi.cn/user/login?mobile=&password=
private OnLogin onLogin;
public void getLogin(String mobile,String psd){
OkHttpClient cilent=new OkHttpClient();
FormBody.Builder builder=new FormBody.Builder();
builder.add("mobile",mobile);
builder.add("password",psd);
FormBody body=builder.build();
Request request=new Request.Builder().post(body).url(path).build();
cilent.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
try {
String result = response.body().string();
Gson gson=new Gson();
LoginBean json = gson.fromJson(result, LoginBean.class);
String code = json.getCode();
String msg = json.getMsg();
System.out.println("=====登录code"+code);
System.out.println("=====登录"+msg);
LoginBean.DataBean data = json.getData();
String uid = data.getUid() + "";
System.out.println("===请求、uid=="+uid);
if(code.equals("0")){
onLogin.getLoginSuccess(code,msg,data);
}else if(code.equals("1")){
onLogin.getLoginFaliure(code,msg);
}
}catch (Exception e){
e.printStackTrace();
}
}
});
}
public void setOnZhuce(OnLogin onZhuce) {
this.onLogin = onZhuce;
}
public interface OnLogin{
void getLoginSuccess(String code, String msg, LoginBean.DataBean list);
void getLoginFaliure(String code, String msg);
void onFaliuer(Call call, IOException e);
}
}
//当然少不了RegModel的类啦
import com.google.gson.Gson;
import java.io.IOException;
import baway.com.denglvzhuce.bean.RegBean;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class RegModel {
private String path="https://www.zhaoapi.cn/user/reg";
//https://www.zhaoapi.cn/user/reg?mobile=&password=
private OnZhuce onZhuce;
public void getResgin(String mobile,String psd){
OkHttpClient cilent=new OkHttpClient();
FormBody.Builder builder=new FormBody.Builder();
builder.add("mobile",mobile);
builder.add("password",psd);
FormBody body=builder.build();
Request request=new Request.Builder().post(body).url(path).build();
cilent.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
try {
String result = response.body().string();
Gson gson=new Gson();
RegBean json = gson.fromJson(result, RegBean.class);
String code = json.getCode();
String msg = json.getMsg();
System.out.println("=====注册code"+code);
System.out.println("=====注册msg"+msg);
if(code.equals("0")){
onZhuce.getZhuceSuccess(code,msg);
}else if(code.equals("1")){
onZhuce.getZhuceFaliure(code,msg);
}
}catch (Exception e){
e.printStackTrace();
}
}
});
}
public void setOnZhuce(OnZhuce onZhuce) {
this.onZhuce = onZhuce;
}
public interface OnZhuce{
void getZhuceSuccess(String code, String msg);
void getZhuceFaliure(String code, String msg);
void onFaliuer(Call call, IOException e);
}
}
//一个登录的LoginPresenter类
import android.content.Context;
import android.text.TextUtils;
import android.widget.Toast;
import java.io.IOException;
import baway.com.denglvzhuce.bean.LoginBean;
import baway.com.denglvzhuce.model.LoginModel;
import baway.com.denglvzhuce.view.LoginView;
import okhttp3.Call;
public class LoginPresenter implements LoginModel.OnLogin {
private LoginView loginView;
private Context context;
private final LoginModel loginModel;
public LoginPresenter(LoginView loginView, Context context) {
this.loginView = loginView;
this.context = context;
loginModel = new LoginModel();
loginModel.setOnZhuce(this);
}
public void requestReagin(String mobile, String psd){
if(TextUtils.isEmpty(mobile)||TextUtils.isEmpty(psd)){
Toast.makeText(context, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
}
loginModel.getLogin(mobile,psd);
}
@Override
public void getLoginSuccess(String code, String msg, LoginBean.DataBean list) {
loginView.getLoginSuccess(code,msg,list);
}
@Override
public void getLoginFaliure(String code, String msg) {
loginView.getLoginFaliure(code, msg);
}
@Override
public void onFaliuer(Call call, IOException e) {
loginView.onFaliuer(call,e);
}
}
//一个注册的RegPresenter类
import android.content.Context;
import android.text.TextUtils;
import android.widget.Toast;
import java.io.IOException;
import baway.com.denglvzhuce.model.RegModel;
import baway.com.denglvzhuce.view.RegView;
import okhttp3.Call;
public class RegPresenter implements RegModel.OnZhuce {
private RegView regView;
private Context context;
private final RegModel regModel;
public RegPresenter(RegView regView, Context context) {
this.regView = regView;
this.context = context;
regModel = new RegModel();
regModel.setOnZhuce(this);
}
public void requestReagin(String mobile, String psd){
if(TextUtils.isEmpty(mobile)||TextUtils.isEmpty(psd)){
Toast.makeText(context, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
}
regModel.getResgin(mobile,psd);
}
@Override
public void getZhuceSuccess(String code, String msg) {
regView.onResginSuccess(code,msg);
}
@Override
public void getZhuceFaliure(String code, String msg) {
regView.onResginFaliure(code,msg);
}
@Override
public void onFaliuer(Call call, IOException e) {
regView.onFaliure(call,e);
}
}
//view包里一个登录的接口
import java.io.IOException;
import baway.com.denglvzhuce.bean.LoginBean;
import okhttp3.Call;
public interface LoginView {
void getLoginSuccess(String code, String msg, LoginBean.DataBean list);
void getLoginFaliure(String code, String msg);
void onFaliuer(Call call, IOException e);
}
//一个注册的接口
import java.io.IOException;
import okhttp3.Call;
public interface RegView {
void onResginSuccess(String code, String msg);
void onResginFaliure(String code, String msg);
void onFaliure(Call call, IOException e);
}
//登录的Activity
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
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.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import baway.com.denglvzhuce.bean.LoginBean;
import baway.com.denglvzhuce.presenter.LoginPresenter;
import baway.com.denglvzhuce.view.LoginView;
import okhttp3.Call;
public class LoginActivity extends AppCompatActivity implements View.OnClickListener, LoginView {
private ImageView login_iv_back;
public static EditText login_et_name;
public static EditText login_et_pass;
private Button login_but_login;
private TextView login_tv_regist;
private TextView login_tv_forget;
private ImageView login_qq_login;
private ImageView login_qq_xx;
private LoginPresenter loginPresenter;
//private SharedPreferences sp;
private SharedPreferences flag;
//private Boolean f=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
flag = getSharedPreferences("flag", Context.MODE_PRIVATE);
initView();
initData();
}
private void initData() {
loginPresenter = new LoginPresenter(LoginActivity.this,this);
// if(TextUtils.isEmpty(login_et_name.getText().toString())&&TextUtils.isEmpty(login_et_pass.getText().toString())){
// login_but_login.setBackgroundColor(Color.RED);
// //System.out.println("=====ogin_et_name.getText().toString()====="+login_et_name.getText().toString());
// SharedUtil instances = SharedUtil.getInstances();
// instances.saveDatad(LoginActivity.this,"boolean",true);
// instances.saveDatad(LoginActivity.this,"login",login_et_name.getText());
// System.out.println("=====login_et_name.getText().toString()======"+login_et_name.getText().toString());
// instances.saveDatad(LoginActivity.this,"loginkey", key);
// }
}
private void initView() {
login_iv_back = (ImageView) findViewById(R.id.login_iv_back);
login_iv_back.setOnClickListener(this);
login_et_name = (EditText) findViewById(R.id.login_et_name);
login_et_pass = (EditText) findViewById(R.id.login_et_pass);
login_but_login = (Button) findViewById(R.id.login_but_login);
login_but_login.setOnClickListener(this);
login_tv_regist = (TextView) findViewById(R.id.login_tv_regist);
login_tv_regist.setOnClickListener(this);
login_tv_forget = (TextView) findViewById(R.id.login_tv_forget);
login_tv_forget.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.login_iv_back:
finish();
break;
case R.id.login_but_login:
String login_et_name = flag.edit().putString("login_et_name", LoginActivity.login_et_name.getText() + "").commit()+"";
String login_et_pass = flag.edit().putString("login_et_pass", LoginActivity.login_et_pass.getText()+"").commit()+"";
System.out.println("===========login_et_name=========="+ login_et_name);
System.out.println("===========login_et_pass==========="+login_et_pass);
if(LoginActivity.login_et_name.length()==11&& LoginActivity.login_et_pass.length()==6){
loginPresenter.requestReagin(LoginActivity.login_et_name.getText().toString(), this.login_et_pass.getText().toString());
// Fragment5.tv.setText(LoginActivity.login_et_name.getText().toString());
// System.out.println("=====login_et_name======"+LoginActivity.login_et_name.getText().toString());
}else {
Toast.makeText(LoginActivity.this,"用户名||密码输入有误",Toast.LENGTH_SHORT).show();
}
break;
case R.id.login_tv_regist:
Intent intent=new Intent(LoginActivity.this,RegActivity.class);
startActivity(intent);
break;
case R.id.login_tv_forget:
Toast.makeText(LoginActivity.this,"忘记密码了你能赖谁,休想找回!!",Toast.LENGTH_SHORT).show();
// TextView tv=new TextView(this);
// tv.setText("");
// tv.setTextColor(Color.RED);
// tv.setTextSize(30);
break;
}
}
@Override
public void getLoginSuccess(final String code, final String msg, final LoginBean.DataBean list) {
if(this!=null) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if(code.equals("0"))
{
Toast.makeText(LoginActivity.this, msg, Toast.LENGTH_SHORT).show();
String uid = list.getUid() + "";
System.out.println("====登陆成功之后获取的uid=="+uid+"");
flag.edit().putString("uid",uid).commit();
Intent intent=new Intent(LoginActivity.this,HomeActivity.class);
//intent.putExtra("yhm",login_et_name.getText());
startActivity(intent);
}
else if(code.equals("1"))
{
Toast.makeText(LoginActivity.this, msg, Toast.LENGTH_SHORT).show();
}
}
});
}
}
@Override
public void getLoginFaliure(String code, String msg) {
}
@Override
public void onFaliuer(Call call, IOException e) {
}
}
//注册的Activity
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.IOException;
import baway.com.denglvzhuce.presenter.RegPresenter;
import baway.com.denglvzhuce.view.RegView;
import okhttp3.Call;
public class RegActivity extends AppCompatActivity implements View.OnClickListener, RegView {
private ImageView reg_iv_back;
private EditText reg_et_name;
private EditText reg_et_pass;
private Button reg_but_zc;
private Button reg_but_dl;
private RegPresenter regPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reg);
initView();
initData();
}
private void initData() {
regPresenter = new RegPresenter(RegActivity.this,this);
if(TextUtils.isEmpty(reg_et_name.getText().toString())&&TextUtils.isEmpty(reg_et_pass.getText().toString())){
reg_but_zc.setBackgroundColor(Color.RED);
}
}
private void initView() {
reg_iv_back = (ImageView) findViewById(R.id.reg_iv_back);
reg_iv_back.setOnClickListener(this);
reg_et_name = (EditText) findViewById(R.id.reg_et_name);
reg_et_pass = (EditText) findViewById(R.id.reg_et_pass);
reg_but_zc = (Button) findViewById(R.id.reg_but_zc);
reg_but_zc.setOnClickListener(this);
reg_but_dl = (Button) findViewById(R.id.reg_but_dl);
reg_but_dl.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.reg_iv_back:
finish();
break;
case R.id.reg_but_zc:
regPresenter.requestReagin(reg_et_name.getText().toString(),reg_et_pass.getText().toString());
break;
case R.id.reg_but_dl:
Intent intent=new Intent(RegActivity.this,LoginActivity.class);
startActivity(intent);
break;
}
}
@Override
public void onResginSuccess(final String code,final String msg) {
if(this!=null) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if(code.equals("0"))
{
Toast.makeText(RegActivity.this, msg, Toast.LENGTH_SHORT).show();
Intent intent=new Intent(RegActivity.this,LoginActivity.class);
startActivity(intent);
}
else if(code.equals("1"))
{
Toast.makeText(RegActivity.this, msg, Toast.LENGTH_SHORT).show();
}
}
});
}
}
@Override
public void onResginFaliure(String code, String msg) {
}
@Override
public void onFaliure(Call call, IOException e) {
}
}
//activity_login的布局文件
//activity_reg的布局文件
//千万别忘记加网络权限哦!!!