You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
214 lines
8.6 KiB
214 lines
8.6 KiB
package com.example.drink_order_system;
|
|
|
|
import android.Manifest;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.content.pm.PackageManager;
|
|
import android.os.Handler;
|
|
import android.os.Message;
|
|
import android.os.Bundle;
|
|
import android.util.Log;
|
|
import android.view.View;
|
|
import android.widget.EditText;
|
|
import android.widget.Toast;
|
|
|
|
import androidx.appcompat.app.AlertDialog;
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
import androidx.core.app.ActivityCompat;
|
|
|
|
import java.io.IOException;
|
|
import okhttp3.Call;
|
|
import okhttp3.FormBody;
|
|
import okhttp3.OkHttpClient;
|
|
import okhttp3.Request;
|
|
import okhttp3.RequestBody;
|
|
import okhttp3.Response;
|
|
|
|
public class MainActivity extends AppCompatActivity {
|
|
public static final int MSG_LOGIN_ERR = 1; //登录错误
|
|
public static final int MSG_CONNET_ERR = 2; //网络链接错误
|
|
public static final int MSG_SIGNUP_ERR = 3;
|
|
public static final int MSG_SIGNUP_SUC = 4;
|
|
private final Context mContext = this;
|
|
private EditText ET_username;
|
|
private EditText ET_password;
|
|
private LoginHandler login_handler;
|
|
String responseData=null;
|
|
public void handlePermission() {
|
|
// 检查是否开启 Manifest.permission.xxx
|
|
// (xxx 为权限,根据自己需求添加)
|
|
if (ActivityCompat.checkSelfPermission(this,
|
|
Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
|
|
Toast.makeText(this, "Permission has been allowed", Toast.LENGTH_SHORT).show();
|
|
} else {
|
|
Toast.makeText(this, "ask for permission", Toast.LENGTH_SHORT).show();
|
|
// 请求权限
|
|
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
login_handler = new LoginHandler();
|
|
setContentView(R.layout.activity_main);
|
|
handlePermission();
|
|
ET_username = findViewById(R.id.et_username);
|
|
ET_password = findViewById(R.id.et_password);
|
|
|
|
}
|
|
|
|
public void BT_signUp_onClick(View v) {
|
|
String username = ET_username.getText().toString();
|
|
System.out.println(username);
|
|
String password = ET_password.getText().toString();
|
|
|
|
String login_check_url = Constant.SIGNUP_URL + "?Cac=" + username + "&Cpw=" + password;
|
|
Log.e("RegisterActivity", login_check_url);
|
|
okhttp3.Callback callback = new okhttp3.Callback() {
|
|
@Override
|
|
public void onResponse(Call call, Response response) throws IOException {
|
|
responseData = response.body().string();
|
|
Log.e("RegisterActivity", "onResponse");
|
|
Log.e("RegisterActivity", responseData);
|
|
Log.e("RegisterActivity", "Response length: " + responseData.length());
|
|
|
|
if (responseData.trim().equals("success")) {
|
|
Message msg = new Message();
|
|
msg.what = MSG_SIGNUP_SUC;
|
|
login_handler.sendMessage(msg);
|
|
Intent intent = new Intent(MainActivity.this, RootActivity.class);
|
|
intent.putExtra("userName", username);
|
|
startActivity(intent);
|
|
finish();
|
|
} else {
|
|
Message msg = new Message();
|
|
msg.what = MSG_SIGNUP_ERR;
|
|
login_handler.sendMessage(msg);
|
|
}
|
|
//showResponse(responseData);
|
|
}
|
|
|
|
@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;
|
|
login_handler.sendMessage(msg);
|
|
//showResponse(responseData);
|
|
}
|
|
};
|
|
OkHttpClient client = new OkHttpClient();
|
|
|
|
Request request = new Request.Builder()
|
|
.url(login_check_url)
|
|
.build();
|
|
//发送请求
|
|
client.newCall(request).enqueue(callback);
|
|
}
|
|
|
|
public void BT_logIn_onClick(View v) {
|
|
String username = ET_username.getText().toString();
|
|
System.out.println(username);
|
|
String password = ET_password.getText().toString();
|
|
// if(username.equals("")||password.equals(""))
|
|
// {
|
|
// Toast.makeText(this, "用户名或密码不能为空(!_!)", Toast.LENGTH_SHORT).show();
|
|
// }
|
|
// else {
|
|
// Account temp = new Account(username, password, mContext);
|
|
// Intent intent;
|
|
// if (temp.exist()) {
|
|
// //System.out.println("success");
|
|
// intent = new Intent(MainActivity.this, RootActivity.class);
|
|
// intent.putExtra("userName", username);
|
|
// startActivity(intent);
|
|
// } else {
|
|
// //System.out.println("fail");
|
|
// Toast.makeText(this, "登录失败 (@_@)\n用户名或密码错误", Toast.LENGTH_SHORT).show();
|
|
// }
|
|
// }
|
|
String login_check_url = Constant.LOGIN_URL + "?Cac=" + username + "&Cpw=" + password;
|
|
Log.e("LoginActivity", login_check_url);
|
|
okhttp3.Callback callback = new okhttp3.Callback() {
|
|
@Override
|
|
public void onResponse(Call call, Response response) throws IOException {
|
|
responseData = response.body().string();
|
|
Log.e("LoginActivity", "onResponse");
|
|
Log.e("LoginActivity", responseData);
|
|
Log.e("LoginActivity", "Response length: " + responseData.length());
|
|
|
|
if (responseData.trim().equals("success")) {
|
|
Intent intent = new Intent(MainActivity.this, RootActivity.class);
|
|
intent.putExtra("userName", username);
|
|
startActivity(intent);
|
|
} else {
|
|
Message msg = new Message();
|
|
msg.what = MSG_LOGIN_ERR;
|
|
login_handler.sendMessage(msg);
|
|
}
|
|
//showResponse(responseData);
|
|
}
|
|
|
|
@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);
|
|
//showResponse(responseData);
|
|
}
|
|
};
|
|
OkHttpClient client = new OkHttpClient();
|
|
|
|
Request request = new Request.Builder()
|
|
.url(login_check_url)
|
|
.build();
|
|
//发送请求
|
|
client.newCall(request).enqueue(callback);
|
|
}
|
|
|
|
class LoginHandler extends Handler {
|
|
@Override
|
|
public void dispatchMessage(Message msg) {
|
|
super.dispatchMessage(msg);
|
|
switch (msg.what) {
|
|
case MSG_LOGIN_ERR:
|
|
ET_username.setText("");
|
|
ET_password.setText("");
|
|
ET_username.requestFocus();
|
|
// Toast.makeText(context, "用户名或密码输入不正确,请重新输入",
|
|
// Toast.LENGTH_SHORT).show();
|
|
new AlertDialog.Builder(MainActivity.this)
|
|
.setTitle("注意")
|
|
.setMessage("用户名或密码输入不正确,请重新输入")
|
|
//.setMessage(responseData)
|
|
.setPositiveButton("确定", null)
|
|
.create()
|
|
.show();
|
|
break;
|
|
case MSG_CONNET_ERR:
|
|
new AlertDialog.Builder(MainActivity.this)
|
|
.setTitle("注意")
|
|
.setMessage("网络连接错误,请检查网络")
|
|
.setPositiveButton("确定", null)
|
|
.create()
|
|
.show();
|
|
break;
|
|
case MSG_SIGNUP_ERR:
|
|
new AlertDialog.Builder(MainActivity.this)
|
|
.setTitle("注意")
|
|
.setMessage("注册失败")
|
|
.setPositiveButton("确定", null)
|
|
.create()
|
|
.show();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
} |