parent
0d165ed2ac
commit
b2bfe25d99
@ -0,0 +1,226 @@
|
|||||||
|
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.Toast;
|
||||||
|
|
||||||
|
import com.example.activity.TestSettingActivity;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import okhttp3.Call;
|
||||||
|
import okhttp3.FormBody;
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
|
import okhttp3.Request;
|
||||||
|
import okhttp3.RequestBody;
|
||||||
|
import okhttp3.Response;
|
||||||
|
|
||||||
|
public class StudentLogin extends AppCompatActivity {
|
||||||
|
Button login,register;
|
||||||
|
EditText sno,password;
|
||||||
|
|
||||||
|
public static final int MSG_LOGIN_ERR = 1; //登录错误
|
||||||
|
public static final int MSG_CONNET_ERR = 2; //网络链接错误
|
||||||
|
private Context context;
|
||||||
|
|
||||||
|
private LoginHandler login_handler;
|
||||||
|
@Override
|
||||||
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
|
|
||||||
|
super.onCreate(savedInstanceState);
|
||||||
|
setContentView(R.layout.login_student);
|
||||||
|
context = this;
|
||||||
|
InitView();
|
||||||
|
login_handler = new LoginHandler();
|
||||||
|
Init();
|
||||||
|
/*super.onCreate(savedInstanceState);
|
||||||
|
setContentView(R.layout.login_student);
|
||||||
|
login = (Button) findViewById(R.id.login_button);
|
||||||
|
register = (Button) findViewById(R.id.reg_button);
|
||||||
|
sno = (EditText) findViewById(R.id.username);
|
||||||
|
password = (EditText) findViewById(R.id.password);
|
||||||
|
login.setOnClickListener(this);
|
||||||
|
register.setOnClickListener(this);
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
private void InitView()
|
||||||
|
{
|
||||||
|
sno = (EditText)findViewById(R.id.username);
|
||||||
|
password = (EditText)findViewById(R.id.password);
|
||||||
|
login = (Button)findViewById(R.id.login_button);
|
||||||
|
register = (Button) findViewById(R.id.reg_button);
|
||||||
|
}
|
||||||
|
private void Init() {
|
||||||
|
|
||||||
|
//设置提示的颜色
|
||||||
|
sno.setHintTextColor(getResources().getColor(R.color.white));
|
||||||
|
password.setHintTextColor(getResources().getColor(R.color.white));
|
||||||
|
|
||||||
|
|
||||||
|
//登录
|
||||||
|
login.setOnClickListener(new View.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(View view) {
|
||||||
|
if (judge()) {
|
||||||
|
login.setEnabled(false);//点了登录后不可以再点,避免用户乱点
|
||||||
|
loginInfo();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
//注册
|
||||||
|
register.setOnClickListener(new View.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(View view) {
|
||||||
|
Toast.makeText(context, "注册", Toast.LENGTH_SHORT).show();
|
||||||
|
startActivity(new Intent(StudentLogin.this, StudentRegister.class));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
private void loginInfo() {
|
||||||
|
String userId = sno.getText().toString();
|
||||||
|
String userPassword = password.getText().toString();
|
||||||
|
//get 请求
|
||||||
|
String login_check_url = "http://169.254.115.53:8080/shixun/Login";
|
||||||
|
RequestBody requestBody = new FormBody.Builder()
|
||||||
|
.add( "username",userId)
|
||||||
|
.add("password",userPassword)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
okhttp3.Callback callback = new okhttp3.Callback()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void onResponse(Call call, Response response) throws IOException {
|
||||||
|
String responseData = response.body().string();
|
||||||
|
Log.e("LoginActivity","onResponse");
|
||||||
|
Log.e("LoginActivity",responseData);
|
||||||
|
if(responseData.equals("success")) {
|
||||||
|
Looper.prepare();
|
||||||
|
Toast.makeText(context, "恭喜您登录成功",Toast.LENGTH_SHORT).show();
|
||||||
|
//startActivity(new Intent(StudentLogin.this, TestSettingActivity.class));
|
||||||
|
//finish();
|
||||||
|
Looper.loop();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Message msg = new Message();
|
||||||
|
msg.what = MSG_LOGIN_ERR;
|
||||||
|
login_handler.sendMessage(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void onFailure(Call call, IOException e) {
|
||||||
|
String responseData = "Error!";
|
||||||
|
Log.e("LoginActivity","Failure");
|
||||||
|
Log.e("LoginActivity",responseData);
|
||||||
|
Message msg = new Message();
|
||||||
|
msg.what = MSG_CONNET_ERR;
|
||||||
|
login_handler.sendMessage(msg);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
OkHttpClient client = new OkHttpClient();
|
||||||
|
|
||||||
|
Request request = new Request.Builder()
|
||||||
|
.url(login_check_url)
|
||||||
|
.post(requestBody)
|
||||||
|
.build();
|
||||||
|
//发送请求
|
||||||
|
client.newCall(request).enqueue(callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
//判断登录信息是否合法
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
3 * 事件捕获
|
||||||
|
4 */
|
||||||
|
class LoginHandler 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(StudentLogin.this)
|
||||||
|
.setTitle("注意")
|
||||||
|
.setMessage("用户名或密码输入不正确,请重新输入")
|
||||||
|
.setPositiveButton("确定",null)
|
||||||
|
.create()
|
||||||
|
.show();
|
||||||
|
login.setEnabled(true);
|
||||||
|
break;
|
||||||
|
case MSG_CONNET_ERR:
|
||||||
|
new AlertDialog.Builder(StudentLogin.this)
|
||||||
|
.setTitle("注意")
|
||||||
|
.setMessage("网络连接错误,请检查网络")
|
||||||
|
.setPositiveButton("确定",null)
|
||||||
|
.create()
|
||||||
|
.show();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
@Override
|
||||||
|
public void onClick(View view) {
|
||||||
|
switch (view.getId()){
|
||||||
|
case R.id.reg_button:
|
||||||
|
Intent intent = new Intent(StudentLogin.this,StudentRegister.class);
|
||||||
|
startActivity(intent);
|
||||||
|
break;
|
||||||
|
case R.id.login_button:
|
||||||
|
new Thread(){
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
LoginDao userDao = new LoginDao();
|
||||||
|
int msg = userDao.login(sno.getText().toString(),password.getText().toString());
|
||||||
|
hand1.sendEmptyMessage(msg);
|
||||||
|
}
|
||||||
|
}.start();
|
||||||
|
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressLint("HandlerLeak")
|
||||||
|
final Handler hand1 = new Handler() {
|
||||||
|
@Override
|
||||||
|
public void handleMessage(Message msg) {
|
||||||
|
if (msg.what == 0){
|
||||||
|
Toast.makeText(getApplicationContext(), "登录失败", Toast.LENGTH_LONG).show();
|
||||||
|
} else if (msg.what == 1) {
|
||||||
|
Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_LONG).show();
|
||||||
|
} else if (msg.what == 2){
|
||||||
|
Toast.makeText(getApplicationContext(), "密码错误", Toast.LENGTH_LONG).show();
|
||||||
|
} else if (msg.what == 3){
|
||||||
|
Toast.makeText(getApplicationContext(), "账号不存在", Toast.LENGTH_LONG).show();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};*/
|
||||||
|
}
|
Loading…
Reference in new issue