parent
b2bfe25d99
commit
62e4decf4c
@ -0,0 +1,269 @@
|
|||||||
|
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 java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
import okhttp3.Call;
|
||||||
|
import okhttp3.FormBody;
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
|
import okhttp3.Request;
|
||||||
|
import okhttp3.RequestBody;
|
||||||
|
import okhttp3.Response;
|
||||||
|
|
||||||
|
public class StudentRegister 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;
|
||||||
|
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.student_register);
|
||||||
|
context = this;
|
||||||
|
reg = (Button) findViewById(R.id.reg_button);
|
||||||
|
password = (EditText) findViewById(R.id.pwd_msg);
|
||||||
|
rpassword = (EditText) findViewById(R.id.rpwd_msg);
|
||||||
|
dept = (EditText) findViewById(R.id.dept_id);
|
||||||
|
name = (EditText) findViewById(R.id.username_msg);
|
||||||
|
sno = (EditText) findViewById(R.id.sno);
|
||||||
|
man = (RadioButton) findViewById(R.id.man);
|
||||||
|
woman = (RadioButton) findViewById(R.id.woman);
|
||||||
|
login = (Button) findViewById(R.id.login_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;
|
||||||
|
sno_str1 = sno.getText().toString();
|
||||||
|
name_str1 = name.getText().toString();
|
||||||
|
pw_str1 = password.getText().toString();
|
||||||
|
dept_str1 = dept.getText().toString();
|
||||||
|
sex_str1 = "man";
|
||||||
|
if(woman.isChecked()){
|
||||||
|
sex_str1 = "woman";
|
||||||
|
}
|
||||||
|
//get 请求
|
||||||
|
String login_check_url = "http://169.254.115.53:8080/shixun/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)
|
||||||
|
.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(StudentRegister.this, StudentLogin.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.login_button:
|
||||||
|
Intent intent = new Intent(StudentRegister.this,StudentLogin.class);
|
||||||
|
startActivity(intent);
|
||||||
|
break;
|
||||||
|
case R.id.reg_button:
|
||||||
|
|
||||||
|
String sno_str, name_str, pw_str, dept_str, sex_str,rpw_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();
|
||||||
|
if(sno_str.length() == 0 ||name_str.length() == 0 ||pw_str.length() == 0
|
||||||
|
||rpw_str.length() == 0 ||dept_str.length() == 0){
|
||||||
|
Toast.makeText(StudentRegister.this, "请将您的所有信息输入全", Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
else if(!TextUtils.equals(password.getText().toString(),rpassword.getText().toString())){
|
||||||
|
Toast.makeText(StudentRegister.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(StudentRegister.this)
|
||||||
|
.setTitle("注意")
|
||||||
|
.setMessage("用户名已存在或注册失败,请重新输入")
|
||||||
|
.setPositiveButton("确定",null)
|
||||||
|
.create()
|
||||||
|
.show();
|
||||||
|
reg.setEnabled(true);
|
||||||
|
break;
|
||||||
|
case MSG_CONNET_ERR:
|
||||||
|
new AlertDialog.Builder(StudentRegister.this)
|
||||||
|
.setTitle("注意")
|
||||||
|
.setMessage("网络连接错误,请检查网络")
|
||||||
|
.setPositiveButton("确定",null)
|
||||||
|
.create()
|
||||||
|
.show();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
LoginDao dao = new LoginDao();
|
||||||
|
Register register = new Register();
|
||||||
|
/*Register uu = dao.findUser(register.getSno());*/
|
||||||
|
|
||||||
|
/*register.setSno(sno_str);
|
||||||
|
|
||||||
|
if(sno_str.length() == 0 ||name_str.length() == 0 ||pw_str.length() == 0
|
||||||
|
||rpw_str.length() == 0 ||dept_str.length() == 0||sex_str.length() ==0 ){
|
||||||
|
Toast.makeText(StudentRegister.this, "请将您的所有信息输入全", Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
else if(!TextUtils.equals(password.getText().toString(),rpassword.getText().toString())){
|
||||||
|
Toast.makeText(StudentRegister.this, "您两次输入的密码不一致,请重新输入", Toast.LENGTH_SHORT).show();
|
||||||
|
password.setText("");
|
||||||
|
rpassword.setText("");
|
||||||
|
password.setFocusable(true);
|
||||||
|
rpassword.setFocusable(true);
|
||||||
|
}
|
||||||
|
else if(sex.getText().toString()!="男"){
|
||||||
|
Toast.makeText(StudentRegister.this, "性别只能输入男或者女", Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
else if(uu!=null){
|
||||||
|
Toast.makeText(StudentRegister.this, "该账户已存在,请重新输入", Toast.LENGTH_SHORT).show();
|
||||||
|
sno.setText("");
|
||||||
|
name.setText("");
|
||||||
|
sno.setFocusable(true);
|
||||||
|
name.setFocusable(true);
|
||||||
|
}*/
|
||||||
|
/*else {
|
||||||
|
new Thread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
Connection conn = null;
|
||||||
|
int u = 0;
|
||||||
|
conn = (Connection) DBUtil.getSQLConnection();
|
||||||
|
String sql = "insert into student(sno,sname,ssex,Dept_ID,s_password) values(?,?,?,?,?)";
|
||||||
|
PreparedStatement pst;
|
||||||
|
try {
|
||||||
|
pst = (PreparedStatement) conn.prepareStatement(sql);
|
||||||
|
//将输入的edit框的值获取并插入到数据库中
|
||||||
|
pst.setString(1, sno.getText().toString());
|
||||||
|
pst.setString(2, name.getText().toString());
|
||||||
|
pst.setString(3, sex.getText().toString());
|
||||||
|
pst.setString(4, dept.getText().toString());
|
||||||
|
pst.setString(5, password.getText().toString());
|
||||||
|
u = pst.executeUpdate();
|
||||||
|
pst.close();
|
||||||
|
conn.close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}).start();
|
||||||
|
Intent intent = new Intent(StudentRegister.this,StudentLogin.class);
|
||||||
|
startActivity(intent);
|
||||||
|
finish();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
});*/
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue