個人作業-學習記錄App開發進度3(註冊介面和註冊介面邏輯)

序章0發表於2024-03-18

繪製註冊頁面,程式碼如下:

<?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/TextAccount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入學號"
android:padding="16dp"
android:layout_margin="16dp"/>

<EditText
android:id="@+id/TextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入密碼"
android:inputType="textPassword"
android:padding="16dp"
android:layout_margin="16dp"/>
<EditText
android:id="@+id/TextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入名字"
android:padding="16dp"
android:layout_margin="16dp"/>
<EditText
android:id="@+id/TextPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入手機號碼"
android:padding="16dp"
android:layout_margin="16dp"/>
<EditText
android:id="@+id/TextClass1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入班級"
android:padding="16dp"
android:layout_margin="16dp"/>
<Button
android:id="@+id/Register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="確認註冊"
android:padding="16dp"
android:layout_margin="16dp"/>
</LinearLayout>
接著編寫註冊頁面的程式碼邏輯
程式碼如下:
package com.example.projects;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
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 Register extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
Button button = findViewById(R.id.Register);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
API api = RetrofitUtils.getRetrofit(AppDataConfig.getURL()).create(API.class);
register(api);
}
});
}
private void register(API api) {

EditText textAccount = findViewById(R.id.TextAccount);
String id = textAccount.getText().toString();

EditText textPassword = findViewById(R.id.TextPassword);
String password = textPassword.getText().toString();

EditText textName = findViewById(R.id.TextName);
String name1 = textName.getText().toString();

EditText textPhone = findViewById(R.id.TextPhone);
String phone = textPhone.getText().toString();

EditText textClass1 = findViewById(R.id.TextClass1);
String class1 = textClass1.getText().toString();

api.getRegister(id,password,name1,phone,class1).enqueue(new Callback<Boolean>() {
@Override
public void onResponse(Call<Boolean> call, Response<Boolean> response) {
if (response.isSuccessful()) {
boolean result = response.body();
Toast.makeText(Register.this, "註冊成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(Register.this, "註冊失敗", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<Boolean> call, Throwable t) {
System.out.println("請求失敗");
System.out.println(t.getMessage());
}
});
}
}
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);
@FormUrlEncoded
@POST("login/register")
Call<Boolean> getRegister(@Field("id") String id,@Field("password") String password,@Field("name1") String name1,@Field("phone") String phone,@Field("class1") String class1);
}
接著是後端程式碼:
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;
}
@PostMapping("/register")
private boolean register(@RequestParam String id, @RequestParam String password,@RequestParam String name1, @RequestParam String phone,@RequestParam String class1) {
boolean result = false;
Login login= new Login(id, password, name1, phone, class1,0);
loginService.save(login);
return result;
}
}
只需要在原先的logincontroller類新增一個新增資料的程式碼了。

相關文章