//登录的方法
public class Main2Activity extends AppCompatActivity implements View.OnClickListener {
private EditText ed_01,ed_02;
private Button login;
private String url="http://www.zhaoapi.cn/user/login";
private TextView register;
private SharedPreferences phonee;
private LoginBean loginBean;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
//初始化控件
ed_01=(EditText)findViewById(R.id.ed_01);
ed_02=(EditText)findViewById(R.id.ed_02);
//登录的id
login=(Button)findViewById(R.id.login);
//注册的id
register=(TextView)findViewById(R.id.register);
login.setOnClickListener(this);
register.setOnClickListener(this);
//保存手机号
phonee = getSharedPreferences("phonee", Context.MODE_PRIVATE);
}
@Override
public void onClick(View view) {
switch (view.getId()){
//登录的方法
case R.id.login:
//获取EditText的手机号
final String phone = ed_01.getText().toString().trim();
//获取EditText的密码
String pass = ed_02.getText().toString().trim();
//网络请求
new OkHttp1().get(url+"?mobile="+phone+"&password="+pass).result(new OkHttp1.HttpLister() {
@Override
public void success(String data) {
//解析数据
loginBean = new Gson().fromJson(data, LoginBean.class);
//保存手机号
phonee.edit().putString("phone",loginBean.getData().getUsername()).commit();
//判断是否为0
if ("0".equals(loginBean.getCode())){
//跳转到另一个页面
Intent intent = new Intent(Main2Activity.this, MainActivity.class);
startActivity(intent);
}else {
Toast.makeText(Main2Activity.this,"错误",Toast.LENGTH_LONG).show();
}
}
});
break;
//注册的方法
case R.id.register:
Intent intent = new Intent(Main2Activity.this, Main4Activity.class);
startActivity(intent);
break;
}
}
}
//注册的方法
public class Main4Activity extends AppCompatActivity {
private EditText ed_01,ed_02;
private Button zhuce;
private String url2="http://www.zhaoapi.cn/user/reg";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
//初始化控件
ed_01=(EditText)findViewById(R.id.ed_01);
ed_02=(EditText)findViewById(R.id.ed_02);
zhuce=(Button)findViewById(R.id.zhuce);
//注册的点击事件
zhuce.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//获取手机号和密码
final String phone = ed_01.getText().toString().trim();
String pass = ed_02.getText().toString().trim();
网络请求
new OkHttp1().get(url2+"?mobile="+phone+"&password="+pass).result(new OkHttp1.HttpLister() {
@Override
public void success(String data) {
//解析数据
ZhuceBean zhuceBean = new Gson().fromJson(data, ZhuceBean.class);
//判断是否为0
if ("0".equals(zhuceBean.getCode())){
//跳转到展示页面
Intent intent = new Intent(Main4Activity.this, Main2Activity.class);
startActivity(intent);
}else {
Toast.makeText(Main4Activity.this,"错误",Toast.LENGTH_LONG).show();
}
}
});
}
});
}
}