个人作业-学习记录App开发进度2(登录界面和登录逻辑)

czf / 2024-03-16 / 原文

首先我们创建一个安卓页面,然后完成登录页面的绘制。代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">

<EditText
android:id="@+id/editTextAccount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入账号"
android:padding="16dp"
android:layout_margin="16dp"/>

<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:inputType="textPassword"
android:padding="16dp"
android:layout_margin="16dp"/>

<Button
android:id="@+id/buttonLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录"
android:padding="16dp"
android:layout_margin="16dp"
/>

<Button
android:id="@+id/buttonRegister"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注册"
android:padding="16dp"
android:layout_margin="16dp"/>

</LinearLayout>
绘制的效果如下:

api类代码:

package com.example.projects.Dao;

import retrofit2.Call;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.POST;

public interface API {
@FormUrlEncoded
@POST("login/login")
Call<Boolean> getLogin(@Field("id") String id,@Field("password") String password);
}

然后我们完成Login即登录页面的逻辑代码编写,代码如下:

package com.example.projects;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.projects.Dao.API;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class Login extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Button button = (Button) findViewById(R.id.buttonLogin);
Button buttonRegister = (Button) findViewById(R.id.buttonRegister);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
API api = RetrofitUtils.getRetrofit(AppDataConfig.getURL()).create(API.class);
login(api);
}
});
buttonRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent it=new Intent(Login.this,Register.class);
startActivity(it);
}
});
}
private void login(API api) {

String id=findViewById(R.id.editTextAccount).toString();
String password=findViewById(R.id.editTextPassword).toString();
api.getLogin(id,password).enqueue(new Callback<Boolean>() {
@Override
public void onResponse(Call<Boolean> call, Response<Boolean> response) {
if (response.isSuccessful()) {
boolean result = response.body();
Intent it=new Intent(Login.this,MainActivity.class);
startActivity(it);
} else {
Toast.makeText(Login.this, "登录失败", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<Boolean> call, Throwable t) {
System.out.println("请求失败");
System.out.println(t.getMessage());
}
});
}
}
这其中我们首先从安卓页面上的文本框获取到了用户输入的学号和密码,然后用Button获取到安卓页面上的按钮视图用于编写点击事件。
点击事件里面用retrofit框架发送请求到后端并且将获取到的数据当做参数传入到后端。接着处理后端返回的数据如果返回的数据为true则跳转到主页,否则在安卓页面底部显示“登录失败”的语句。
当然之后我们如果想要改进的话,还可以修改后端代码用以判断为什么登录失败是账号不存在还是密码错误,来给用户明确的错误提示。不过这里就先这样吧。
当然页面上面还有一个注册按钮,我们也是需要获得这个按钮视图,然后编写点击事件,点击事件就是直接跳转到注册页面。当然我们需要提前写好注册页面以及逻辑代码(这个我们下次博客再写)。
接下来就是后端代码,后端我用的是springboot+mybatisplus架构去写的,仅供参考。
首先定义一个实体类,用于对参数的操作,代码如下:
package com.example.projectsweb.entity;

import java.io.Serializable;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
* <p>
*
* </p>
*
* @author projectsweb
* @since 2024-03-11
*/
@Data
@EqualsAndHashCode(callSuper = false)
public class Login implements Serializable {

private static final long serialVersionUID = 1L;

private String id;

private String password;

private String name1;

private String phone;

private String class1;

private int identity;
public Login() {
}

public Login(String id, String password, String name1, String phone, String class1,int identity) {
this.id = id;
this.password = password;
this.name1 = name1;
this.phone = phone;
this.class1 = class1;
this.identity = identity;
}

public int getIdentity() {
return identity;
}

public void setIdentity(int identity) {
this.identity = identity;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getName1() {
return name1;
}

public void setName1(String name1) {
this.name1 = name1;
}

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}

public String getClass1() {
return class1;
}

public void setClass1(String class1) {
this.class1 = class1;
}
}
我用的是mybatisplus的代码生成器生成的项目结构如下(其中的teacher那个和这个项目无关,就当没看见就好):

 自动生成完项目结构和写好实体类定义之后开始写controller类,代码如下:

package com.example.projectsweb.controller;


import com.example.projectsweb.entity.Login;
import com.example.projectsweb.service.impl.LoginServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* <p>
* 前端控制器
* </p>
*
* @author projectsweb
* @since 2024-03-11
*/
@RestController
@RequestMapping("/login")
public class LoginController {
@Autowired
private LoginServiceImpl loginService;
@PostMapping("/login")
private boolean login(@RequestParam String id, @RequestParam String password) {
boolean result = false;

// 在数据库中查询是否存在匹配的id和password
Login login = loginService.lambdaQuery()
.select(Login::getId, Login::getPassword)
.eq(Login::getId, id)
.eq(Login::getPassword, password)
.one();
if (login != null) {
result = true;
}
return result;
}

}
这个代码就不用解释了吧,就是通过对id和password的匹配在数据库中查询是否有数据,如果有返回true,没有返回false。这样整个登录功能就算是完成了。