从网络请求数据,实现注册登录

登录activity
public class LoginActivity extends AppCompatActivity implements View.OnClickListener{
TextView login_name,login_pwd,register_btn;
Button login_btn;
String login= Api.api+”act=login”;
int i=1;
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case 0:
String s= (String) msg.obj;
Log.i(“tag”,s);
Gson gson=new Gson();
LoginBean loginBean=gson.fromJson(s,LoginBean.class);
int code=loginBean.getCode();
if(code==200){
SharedUtil instances=SharedUtil.getInstances();
instances.saveDatad(LoginActivity.this,”flag”,true);
instances.saveDatad(LoginActivity.this,”username”,loginBean.getDatas().getUsername());
instances.saveDatad(LoginActivity.this,”userid”,loginBean.getDatas().getUserid());
instances.saveDatad(LoginActivity.this,”userkey”,loginBean.getDatas().getKey());
handler.postDelayed(new Runnable() {
@Override
public void run() {
if(1==i){
handler.removeCallbacksAndMessages(null);
finish();
}
login_btn.setText(“登陆中…”+i+”秒后跳转”);
Intent intent=new Intent(LoginActivity.this,AnotherActivity.class);
startActivity(intent);
i–;
handler.postDelayed(this,1000);
}
},1000);
}else{
Toast.makeText(LoginActivity.this, “登陆失败”, Toast.LENGTH_SHORT).show();
}
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
login_btn = (Button) findViewById(R.id.login_btn);
login_name = (TextView) findViewById(R.id.login_name);
login_pwd = (TextView) findViewById(R.id.login_pwd);
register_btn = (TextView) findViewById(R.id.register_btn);
getTextListener();
login_btn.setOnClickListener(this);
register_btn.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.login_btn:
String name=login_name.getText().toString();
String pwd=login_pwd.getText().toString();
Intent intent = new Intent();
setResult(200,intent);
login(name,pwd);
break;
case R.id.register_btn:
Intent register=new Intent(LoginActivity.this,RegisterActivity.class);
startActivityForResult(register,200);
break;
}
}
private void login(String name, String pwd) {
//()代表尖括号
Map(String,String) params=new HashMap()();
params.put(“username”,name);
params.put(“password”,pwd);
params.put(“client”,”android”);
OkHttp3Utils.doPost(login, params, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String s=response.body().string();
Message message = new Message();
message.what=0;
message.obj=s;
handler.sendMessage(message);
}
});
}
public void getTextListener() {
login_btn.setEnabled(Boolean.FALSE);
login_name.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
String name=login_name.getText().toString();
String pwd=login_pwd.getText().toString();
if(TextUtils.isEmpty(name)||TextUtils.isEmpty(pwd)){
login_btn.setEnabled(Boolean.FALSE);
}else{
login_btn.setEnabled(Boolean.TRUE);
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
login_pwd.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
String name=login_name.getText().toString();
String pwd=login_pwd.getText().toString();
if(TextUtils.isEmpty(name)||TextUtils.isEmpty(pwd)){
login_btn.setEnabled(Boolean.FALSE);
Toast.makeText(LoginActivity.this, “用戶名/密碼為空”, Toast.LENGTH_SHORT).show();
}else{
login_btn.setEnabled(Boolean.TRUE);
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==201){
String name=data.getStringExtra(“name”);
login_name.setText(name);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
handler.removeCallbacksAndMessages(null);
}
}

注册activity
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener{
private EditText login_name;
private EditText login_pwd;
private EditText login_pwd2;
private EditText login;
private Button login_btn;
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case 0:
String obj= (String) msg.obj;
Log.i(“注册成功”,obj);
Gson gson = new Gson();
ZhuCeBean zhuCeBean=gson.fromJson(obj,ZhuCeBean.class);
int code=zhuCeBean.getCode();
if(code==200){
String username=zhuCeBean.getDatas().getUsername();
Intent intent = new Intent();
intent.putExtra(“name”,username);
setResult(201,intent);
Toast.makeText(RegisterActivity.this, “注册成功,稍后跳转登陆页面”, Toast.LENGTH_SHORT).show();
handler.postDelayed(new Runnable() {
@Override
public void run() {
finish();
}
},5000);
}
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_register);
login_name = (EditText) findViewById(R.id.login_name);
login_pwd = (EditText) findViewById(R.id.login_pwd);
login_pwd2 = (EditText) findViewById(R.id.login_pwd2);
login = (EditText) findViewById(R.id.login_email);
login_btn = (Button) findViewById(R.id.re_btn);
login_btn.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.re_btn:
getEdData();
break;
}
}
public void getEdData() {
String name = login_name.getText().toString();
String pwd = login_pwd.getText().toString();
String pwd2 = login_pwd2.getText().toString();
String email = login.getText().toString();
//()代表尖括号
final Map(String,String)map=new HashMap()();
String register= Api.api+”act=login&op=register”;
map.put(“username”, name);
map.put(“password”, pwd);
map.put(“password_confirm”, pwd2);
map.put(“email”, email);
map.put(“client”, “android”);
OkHttp3Utils.doPost(register, map, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String string=response.body().string();
Message message = new Message();
message.what=0;
message.obj=string;
handler.sendMessage(message);
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
handler.removeCallbacksAndMessages(null);
}
}

你可能感兴趣的:(从网络请求数据,实现注册登录)