一、在Application启动的时候,创建实例
import android.app.Application;
import android.content.Context;
import com.litesuits.orm.LiteOrm;
public class MyApplication extends Application {
public static LiteOrm liteOrm;
@Override
public void onCreate() {
super.onCreate();
if (liteOrm == null) {
liteOrm = LiteOrm.newSingleInstance(this, "liteorm.db");
}
// open the log
liteOrm.setDebugged(true);
}
}
二、bean类添加注解
@Table("user") @Data @NoArgsConstructor public class User { @PrimaryKey(AssignType.AUTO_INCREMENT) private int id; @NotNull private String username; @NotNull private String password; private int rememberPwdIsCheck; public User(int id,String username,String password,int rememberPwdIsCheck) { this.id = id; this.username = username; this.password = password; this.rememberPwdIsCheck = rememberPwdIsCheck; } }
三、增删改查使用
liteOrm = MyApplication.liteOrm;
User user = liteOrm.queryById(1, User.class);
liteOrm.save(userInfo);
liteOrm.update(user);
liteOrm.delete(user);
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.KeyEvent;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import com.example.soft_test_helper.MyApplication;
import com.example.soft_test_helper.R;
import com.example.soft_test_helper.common.WebServer;
import com.example.soft_test_helper.pojo.User;
import com.example.soft_test_helper.utils.SpUtils;
import com.litesuits.orm.LiteOrm;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import okhttp3.Call;
import okhttp3.MediaType;
public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
LiteOrm liteOrm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
usernameET = findViewById(R.id.usernameET);
passwordET = findViewById(R.id.passwordET);
rememberPwdCB = findViewById(R.id.rememberPwdCB);
forgetPwdTV = findViewById(R.id.forgetPwdTV);
viewPwd = findViewById(R.id.viewPwd);
viewPwd.setOnClickListener(this);
findViewById(R.id.loginBtn).setOnClickListener(this);
findViewById(R.id.registerBtn).setOnClickListener(this);
forgetPwdTV.setOnClickListener(this);
liteOrm = MyApplication.liteOrm;
User user = liteOrm.queryById(1, User.class);
if(user != null) {
rememberPwdCB.setChecked(true);
usernameET.setText(user.getUsername());
passwordET.setText(user.getPassword());
}
}
@Override
public void onClick(View v) {
if(v.getId() == R.id.loginBtn) {
Editable username = usernameET.getText();
Editable pwd = passwordET.getText();
Pattern usernamePattern = Pattern.compile("^[a-zA-Z][a-zA-Z0-9_]{4,15}$");
Matcher usernameMatcher = usernamePattern.matcher(username.toString());
Pattern emailPattern = Pattern.compile("\\w[-\\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\\.)+[A-Za-z]{2,14}");
Matcher emailMatcher = emailPattern.matcher(username.toString());
Pattern pwdPattern = Pattern.compile("^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,15}$");
Matcher pwdMatcher = pwdPattern.matcher(pwd.toString());
boolean usernameIsMatch = usernameMatcher.matches(); //帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线)
boolean emailIsMatch = emailMatcher.matches();//邮箱格式
boolean pwdIsMatch = pwdMatcher.matches(); //强密码(必须包含大小写字母和数字的组合,可以使用特殊字符,长度在8-15之间)
if(!(usernameIsMatch || emailIsMatch)){
Toast.makeText(this,"用户名或邮箱格式验证不合法:\n用户名字母开头,允许5-16字节,允许字母数字下划线",Toast.LENGTH_LONG).show();
return;
}
if(!pwdIsMatch) {
Toast.makeText(this,"密码必须包含大小写字母和数字的组合,可以使用特殊字符,长度在8-15之间",Toast.LENGTH_LONG).show();
return;
}
final String usernameInfo = username.toString().replaceAll("\\s+","");
final String passwordInfo = pwd.toString().replaceAll("\\s+","");
JSONObject userDTO = new JSONObject();
try {
userDTO.put("username",usernameInfo);
userDTO.put("password",passwordInfo);
if(usernameIsMatch) {
userDTO.put("loginType",1);
}else if(emailIsMatch) {
userDTO.put("loginType",4);
}
} catch (JSONException e) {
e.printStackTrace();
}
OkHttpUtils.postString()
.url("http://" + WebServer.SERVER_IP+":"+WebServer.SERVER_PORT+"/user/login/android")
.mediaType(MediaType.parse("application/json; charset=utf-8"))
.content(userDTO.toString())
.build().execute(new StringCallback() {
@Override
public void onError(Call call, Exception e, int id) {
Toast.makeText(LoginActivity.this,"登录失败",Toast.LENGTH_SHORT).show();
}
@Override
public void onResponse(String response, int id) {
Log.e("登录成功", response );
try {
JSONObject respObj = new JSONObject(response);
if(respObj.optString("code").equals("200")) {
User user = liteOrm.queryById(1, User.class);
if(rememberPwdCB.isChecked()) {
if(user == null) {
User userInfo = new User(1,usernameInfo,passwordInfo,0);
liteOrm.save(userInfo);
}else{
user.setUsername(usernameInfo);
user.setPassword(passwordInfo);
user.setRememberPwdIsCheck(1);
liteOrm.update(user);
}
}else {
if(user != null){
liteOrm.delete(user);
}
}
JSONObject dataObj = respObj.optJSONObject("data");
String token = dataObj.optString("token");
SpUtils.put(LoginActivity.this,"token",token);
SpUtils.put(LoginActivity.this,"username",dataObj.optString("username"));
Intent intent = new Intent(LoginActivity.this, PagerActivity.class);
startActivity(intent);
finishAffinity();
}else {
Toast.makeText(LoginActivity.this,"登录失败",Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}else if(v.getId() == R.id.registerBtn) {
Intent intent = new Intent(this, RegisterActivity.class);
startActivity(intent);
}else if(v == viewPwd) {
isShowPwd = !isShowPwd;
//设置密码为明文
if(isShowPwd) {
passwordET.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
}else{
//设置为暗文
passwordET.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
}else if(v == forgetPwdTV) {
Intent intent = new Intent(this,ForgetPasswordActivity.class);
startActivity(intent);
}
}
}