parent
77bb37e61f
commit
8bba189366
@ -0,0 +1,208 @@
|
||||
package com.example;
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
import android.app.AlertDialog;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.os.Message;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.RadioButton;
|
||||
import android.widget.Toast;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import okhttp3.Call;
|
||||
import okhttp3.FormBody;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.Response;
|
||||
|
||||
public class TeacherRegister extends AppCompatActivity implements View.OnClickListener {
|
||||
Button reg,login;
|
||||
public static final int MSG_LOGIN_ERR = 1; //登录错误
|
||||
public static final int MSG_CONNET_ERR = 2; //网络链接错误
|
||||
EditText name,password,rpassword,dept,sno,title;
|
||||
RadioButton sex;
|
||||
String ssex;
|
||||
RadioButton man,woman;
|
||||
private Context context;
|
||||
|
||||
private RegisterHandler register_handler;
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.teacher_register);
|
||||
context = this;
|
||||
reg = (Button) findViewById(R.id.treg_button);
|
||||
password = (EditText) findViewById(R.id.tpwd_msg);
|
||||
rpassword = (EditText) findViewById(R.id.trpwd_msg);
|
||||
dept = (EditText) findViewById(R.id.tdept_id);
|
||||
name = (EditText) findViewById(R.id.tusername_msg);
|
||||
sno = (EditText) findViewById(R.id.tno);
|
||||
man = (RadioButton) findViewById(R.id.tman);
|
||||
woman = (RadioButton) findViewById(R.id.twoman);
|
||||
title = (EditText) findViewById(R.id.ttitle);
|
||||
|
||||
login = (Button) findViewById(R.id.tlogin_button);
|
||||
register_handler = new RegisterHandler();
|
||||
|
||||
reg.setOnClickListener(this);
|
||||
login.setOnClickListener(this);
|
||||
}
|
||||
private void registerInfo() {
|
||||
String sno_str1, name_str1, pw_str1, dept_str1, sex_str1,title1;
|
||||
sno_str1 = sno.getText().toString();
|
||||
name_str1 = name.getText().toString();
|
||||
pw_str1 = password.getText().toString();
|
||||
dept_str1 = dept.getText().toString();
|
||||
title1 = title.getText().toString();
|
||||
sex_str1 = "man";
|
||||
if(woman.isChecked()){
|
||||
sex_str1 = "woman";
|
||||
}
|
||||
//get 请求
|
||||
String login_check_url = "http://169.254.115.53:8080/shixun/T_Register";
|
||||
RequestBody requestBody = new FormBody.Builder()
|
||||
.add( "sno",sno_str1)
|
||||
.add("name",name_str1)
|
||||
.add("dept",dept_str1)
|
||||
.add("password",pw_str1)
|
||||
.add("sex",sex_str1)
|
||||
.add("title",title1)
|
||||
.build();
|
||||
|
||||
okhttp3.Callback callback = new okhttp3.Callback()
|
||||
{
|
||||
@Override
|
||||
public void onResponse(Call call, Response response) throws IOException {
|
||||
String responseData = response.body().string();
|
||||
Log.e("RegisterActivity","onResponse");
|
||||
Log.e("RegisterActivity",responseData);
|
||||
if(responseData.equals("success")) {
|
||||
Looper.prepare();
|
||||
Toast.makeText(context, "注册成功!",Toast.LENGTH_SHORT).show();
|
||||
startActivity(new Intent(TeacherRegister.this, TeacherLogin.class));
|
||||
finish();
|
||||
Looper.loop();
|
||||
}
|
||||
else {
|
||||
Message msg = new Message();
|
||||
msg.what = MSG_LOGIN_ERR;
|
||||
register_handler.sendMessage(msg);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void onFailure(Call call, IOException e) {
|
||||
String responseData = "Error!";
|
||||
Log.e("RegisterActivity","Failure");
|
||||
Log.e("RegisterActivity",responseData);
|
||||
Message msg = new Message();
|
||||
msg.what = MSG_CONNET_ERR;
|
||||
register_handler.sendMessage(msg);
|
||||
}
|
||||
};
|
||||
OkHttpClient client = new OkHttpClient();
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.url(login_check_url)
|
||||
.post(requestBody)
|
||||
.build();
|
||||
//发送请求
|
||||
client.newCall(request).enqueue(callback);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
switch (view.getId()){
|
||||
case R.id.tlogin_button:
|
||||
Intent intent = new Intent(TeacherRegister.this,TeacherLogin.class);
|
||||
startActivity(intent);
|
||||
break;
|
||||
case R.id.treg_button:
|
||||
|
||||
String sno_str, name_str, pw_str, dept_str, sex_str,rpw_str,title_str;
|
||||
sno_str = sno.getText().toString();
|
||||
name_str = name.getText().toString();
|
||||
pw_str = password.getText().toString();
|
||||
dept_str = dept.getText().toString();
|
||||
rpw_str = rpassword.getText().toString();
|
||||
title_str = title.getText().toString();
|
||||
if(sno_str.length() == 0 ||name_str.length() == 0 ||pw_str.length() == 0
|
||||
||rpw_str.length() == 0 ||dept_str.length() == 0||title_str.length() == 0){
|
||||
Toast.makeText(TeacherRegister.this, "请将您的所有信息输入全", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
else if(!TextUtils.equals(password.getText().toString(),rpassword.getText().toString())){
|
||||
Toast.makeText(TeacherRegister.this, "您两次输入的密码不一致,请重新输入", Toast.LENGTH_SHORT).show();
|
||||
password.setText("");
|
||||
rpassword.setText("");
|
||||
password.setFocusable(true);
|
||||
rpassword.setFocusable(true);
|
||||
}
|
||||
else {
|
||||
reg.setEnabled(false);//点了登录后不可以再点,避免用户乱点
|
||||
registerInfo();
|
||||
}
|
||||
break;
|
||||
default:
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//判断注册信息是否合法
|
||||
/*private boolean judge() {
|
||||
if (TextUtils.isEmpty(sno.getText().toString()) ) {
|
||||
Toast.makeText(context, "用户名不能为空",Toast.LENGTH_SHORT).show();
|
||||
return false;
|
||||
} else if (TextUtils.isEmpty(password.getText().toString())) {
|
||||
Toast.makeText(context, "密码不能为空",Toast.LENGTH_SHORT).show();
|
||||
return false;
|
||||
} else if(!TextUtils.equals(password.getText().toString(),rpassword.getText().toString())){
|
||||
Toast.makeText(context, "两次输入的密码不一致",Toast.LENGTH_SHORT).show();
|
||||
return false;
|
||||
}else if(TextUtils.isEmpty(dept.getText().toString())){
|
||||
Toast.makeText(context, "所在院系不能为空",Toast.LENGTH_SHORT).show();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}*/
|
||||
|
||||
/**
|
||||
3 * 事件捕获
|
||||
4 */
|
||||
class RegisterHandler extends Handler {
|
||||
@Override
|
||||
public void dispatchMessage(Message msg) {
|
||||
super.dispatchMessage(msg);
|
||||
switch (msg.what) {
|
||||
case MSG_LOGIN_ERR:
|
||||
sno.setText("");
|
||||
password.setText("");
|
||||
sno.requestFocus();
|
||||
new AlertDialog.Builder(TeacherRegister.this)
|
||||
.setTitle("注意")
|
||||
.setMessage("用户名已存在或注册失败,请重新输入")
|
||||
.setPositiveButton("确定", null)
|
||||
.create()
|
||||
.show();
|
||||
reg.setEnabled(true);
|
||||
break;
|
||||
case MSG_CONNET_ERR:
|
||||
new AlertDialog.Builder(TeacherRegister.this)
|
||||
.setTitle("注意")
|
||||
.setMessage("网络连接错误,请检查网络")
|
||||
.setPositiveButton("确定", null)
|
||||
.create()
|
||||
.show();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}}
|
Loading…
Reference in new issue