后端是基于SpringBoot实现,提供一套标准的RESTful API
Android端通过Retrofit进行网络请求
下面分述三个模块:
一、登录:
Android端部分代码:
private void login(String username, String password) {
Map<String,Object> map = new HashMap<>();
map.put("username",username);
map.put("password",password);
RequestBody body = RequestUtil.getRequestBody(map);
WebApiService service = MyRetrofit.getRetrofit().create(WebApiService.class);
Call<BaseResult<User>> call = service.login(body);
call.enqueue(new Callback<BaseResult<User>>() {
@Override
public void onResponse(Call<BaseResult<User>> call, Response<BaseResult<User>> response) {
BaseResult<User> result = response.body();
assert result != null;
if (result.getCode() == 0){
User user = result.getData();
if (user != null){
Integer id = user.getId();
SPUtil.saveInt(LoginActivity.this,"userId",id);
}
Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_LONG).show();
startActivity(new Intent(LoginActivity.this,MainActivity.class));
finish();
}else {
Toast.makeText(LoginActivity.this,result.getMessage() + "",Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Call<BaseResult<User>> call, Throwable t) {
}
});
}
后端部分代码:
@PostMapping("login")
public Object login(@RequestBody String body) {
String username = JacksonUtil.parseString(body, "username");
String password = JacksonUtil.parseString(body, "password");
if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
return ResponseUtil.badArgument();
}
User user = userService.login(username, password);
if (user == null) {
return ResponseUtil.fail(ResponseCode.AUTH_INVALID_ACCOUNT, "用户名密码错误");
}
return ResponseUtil.ok(user);
}
二、注册:
Android端部分代码:
private RequestBody getValidData() {
String username = mUsernameEdt.getText().toString();
String password = mPasswordEdt.getText().toString();
String nickName = mNickNameEdt.getText().toString();
String dateOfBirth = mDateOfBirthEdt.getText().toString();
String pwdProtectionVal = mAnswerEdt.getText().toString();
if (TextUtils.isEmpty(username)) {
Toast.makeText(this, "用户名不能为空", Toast.LENGTH_LONG).show();
return null;
}
if (TextUtils.isEmpty(password)) {
Toast.makeText(this, "密码不能为空", Toast.LENGTH_LONG).show();
return null;
}
if (TextUtils.isEmpty(nickName)) {
Toast.makeText(this, "昵称不能为空", Toast.LENGTH_LONG).show();
return null;
}
if (TextUtils.isEmpty(dateOfBirth)) {
Toast.makeText(this, "出生日期不能为空", Toast.LENGTH_LONG).show();
return null;
}
if (mPwdProtectionId == -1) {
Toast.makeText(this, "请选择密保问题", Toast.LENGTH_LONG).show();
return null;
}
if (TextUtils.isEmpty(pwdProtectionVal)) {
Toast.makeText(this, "密保答案", Toast.LENGTH_LONG).show();
return null;
}
Map<String, Object> map = new HashMap<>();
map.put("username", username);
map.put("password", password);
map.put("nickName", nickName);
map.put("sex", mSex);
map.put("dateOfBirth", dateOfBirth);
map.put("pwdProtectionId", mPwdProtectionId);
map.put("pwdProtectionVal", pwdProtectionVal);
return RequestUtil.getRequestBody(map);
}
//密保问题
private void getQuestionList() {
WebApiService service = MyRetrofit.getRetrofit().create(WebApiService.class);
Call<BaseResult<JsonObject>> call = service.getQuestions();
call.enqueue(new Callback<BaseResult<JsonObject>>() {
@Override
public void onResponse(Call<BaseResult<JsonObject>> call, Response<BaseResult<JsonObject>> response) {
BaseResult<JsonObject> result = response.body();
if (result == null) {
return;
}
if (result.getCode() == 0) {
JsonObject data = result.getData();
if (data != null && data.has("list")) {
String json = data.get("list").getAsJsonArray().toString();
mQuestions = new Gson().fromJson(json, new TypeToken<List<Question>>() {
}.getType());
mAdapter.refreshUi(mQuestions);
}
}
}
@Override
public void onFailure(Call<BaseResult<JsonObject>> call, Throwable t) {
}
});
}
//去api取得结果
private void register() {
RequestBody body = getValidData();
if (body == null) {
return;
}
WebApiService service = MyRetrofit.getRetrofit().create(WebApiService.class);
Call<BaseResult<Object>> call = service.register(body);
call.enqueue(new Callback<BaseResult<Object>>() {
@Override
public void onResponse(Call<BaseResult<Object>> call, Response<BaseResult<Object>> response) {
BaseResult<Object> result = response.body();
assert result != null;
if (result.getCode() == 0) {
Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_LONG).show();
startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
finish();
} else {
Toast.makeText(RegisterActivity.this, "" + result.getMessage(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Call<BaseResult<Object>> call, Throwable t) {
}
});
}
后端部分代码:
@PostMapping("register")
public Object register(@RequestBody User user) {
if (user == null) {
return ResponseUtil.badArgument();
}
if (StringUtils.isEmpty(user.getUsername()) || StringUtils.isEmpty(user.getPassword()) || StringUtils.isEmpty(user.getNickName()) || StringUtils.isEmpty(user.getDateOfBirth()) || StringUtils.isEmpty(user.getPwdProtectionVal())) {
return ResponseUtil.badArgument();
}
String username = user.getUsername();
List<User> users = userService.queryByUserName(username);
if (users.size() > 0){
return ResponseUtil.fail(ResponseCode.AUTH_NAME_REGISTERED,"用户名已注册");
}
if (userService.register(user) == 0){
return ResponseUtil.updatedDataFailed();
}
return ResponseUtil.ok();
}
三、忘记密码:
Android端部分代码:
try {
RequestBody body = getValidData();
if (body == null){
return;
}
WebApiService service = MyRetrofit.getRetrofit().create(WebApiService.class);
Call<BaseResult<Object>> call = service.forgetPwd(body);
call.enqueue(new Callback<BaseResult<Object>>() {
@Override
public void onResponse(Call<BaseResult<Object>> call, Response<BaseResult<Object>> response) {
BaseResult<Object> result = response.body();
assert result != null;
if (result.getCode() == 0){
Toast.makeText(ForgetPwdActivity.this,"修改密码成功",Toast.LENGTH_LONG).show();
startActivity(new Intent(ForgetPwdActivity.this,LoginActivity.class));
finish();
}else {
Toast.makeText(ForgetPwdActivity.this,result.getMessage() + "",Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Call<BaseResult<Object>> call, Throwable t) {
}
});
} catch (Exception e) {
}
}
后端部分代码:
@PutMapping("forgetPwd")
public Object forgetPwd(@RequestBody String body){
String username = JacksonUtil.parseString(body,"username");
String password = JacksonUtil.parseString(body,"password");
Integer pwdProtectionId = JacksonUtil.parseInteger(body,"pwdProtectionId");
String pwdProtectionVal = JacksonUtil.parseString(body,"pwdProtectionVal");
if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password) || StringUtils.isEmpty(pwdProtectionVal) || pwdProtectionId == null){
return ResponseUtil.badArgument();
}
List<User> users = userService.queryByUserName(username);
if (users.size() == 0){
return ResponseUtil.fail(ResponseCode.AUTH_INVALID_ACCOUNT,"没有找到该用户");
}
if (users.size() > 1){
return ResponseUtil.serious();
}
User user = users.get(0);
Integer _pwdProtectionId = user.getPwdProtectionId();
String _pwdProtectionVal = user.getPwdProtectionVal();
if (_pwdProtectionId == null || _pwdProtectionVal == null){
return ResponseUtil.serious();
}
if (!_pwdProtectionId.equals(pwdProtectionId) || !_pwdProtectionVal.equals(pwdProtectionVal)){
return ResponseUtil.fail(ResponseCode.AUTH_PWD_PROTECTION_UNMATCH,"密保错误");
}
user.setPassWord(password);
if (userService.updateById(user) == 0){
return ResponseUtil.updatedDataFailed();
}
return ResponseUtil.ok();
}
下载完整demo后需要修改的配置:
后端部分主要是数据库配置修改
application-dev.yml
spring:
datasource:
username: root
password: root123
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
以上为数据库配置,要修改为你的数据库用户名密码,其中3306端口后面的test,是数据库名称,也要根据你自己的来。关于MySql的配置,可以参考网上其他教程
Android端部分主要是接口地址修改:
public class MyRetrofit {
public static Retrofit getRetrofit() {
OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
return new Retrofit.Builder()
.baseUrl("http://192.168.43.72:8080/api/")
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
}
将baseurl换成你的地址,即本机局域网ip地址,因此前后端要注意连接到同一网络!
以上即最基本的登录注册修改密码功能,学会这些就可以着手开发更多。后面有时间打算出个Vue版本的前端,有兴趣可以关注。
完成前后端demo下载:https://download.csdn.net/download/always_and_forever_/77847634