国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Android前端+Spring Boot后端 登錄功能實(shí)現(xiàn)

這篇具有很好參考價(jià)值的文章主要介紹了Android前端+Spring Boot后端 登錄功能實(shí)現(xiàn)。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

1.后端比較熟悉,先說(shuō)后端框架搭建

(1)創(chuàng)建項(xiàng)目,總體框架

創(chuàng)建項(xiàng)目后,自己添加包,框架如下

android與spring,學(xué)習(xí)生活,前端,maven,java,spring boot,intellij idea

android與spring,學(xué)習(xí)生活,前端,maven,java,spring boot,intellij idea?android與spring,學(xué)習(xí)生活,前端,maven,java,spring boot,intellij idea

userController里的一些內(nèi)容,只供參考,代碼不全,無(wú)法實(shí)現(xiàn)

@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用戶登錄類(lèi)
     */
    @PostMapping("/login")
    public R<User> login(@RequestBody User user, HttpSession session){
        // 查詢數(shù)據(jù)庫(kù)
        final LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUserName, user.getUserName());
        final User user1 = userService.getOne(queryWrapper);
        // 沒(méi)有根據(jù)賬號(hào)找到數(shù)據(jù)
        if (user1 == null) {
            return R.error("賬號(hào)不存在");
        }
        // 密碼比對(duì)
        if (!user1.getUserPassword().equals(user.getUserPassword())) {
            return R.error("密碼錯(cuò)誤");
        }
        // 登陸成功,保存userID
        session.setAttribute("userName", user.getUserName());
        log.info("當(dāng)前登錄用戶name:" + user.getUserName());
        
        return R.success(user1);

    }
}

(2)創(chuàng)建數(shù)據(jù)庫(kù)

數(shù)據(jù)庫(kù)是直接在社區(qū)版IDEA里連接Mysql,在控制臺(tái)端創(chuàng)建的數(shù)據(jù)庫(kù)和user表,用于數(shù)據(jù)交互。

android與spring,學(xué)習(xí)生活,前端,maven,java,spring boot,intellij idea

2.Android前端

(1)前端主要框架

Activity包里是Activity Java類(lèi),主要響應(yīng)layout包里activity_login.xml等頁(yè)面布局內(nèi)的按鈕響應(yīng)

android與spring,學(xué)習(xí)生活,前端,maven,java,spring boot,intellij idea

(2)寫(xiě)登錄功能

activity_login.xml里的內(nèi)容,只參考界面布局代碼即可,代碼不可用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:gravity="bottom|center"
    android:orientation="vertical"
    tools:context=".Activity.LoginActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="122dp"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:gravity="bottom|center"
            android:paddingBottom="15dp"
            android:text="登錄"
            android:textColor="#0E0D0B"
            android:textSize="34sp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <EditText
            android:id="@+id/Username"
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:layout_marginLeft="40dp"
            android:layout_marginTop="40dp"
            android:layout_marginRight="30dp"
            android:layout_marginBottom="20dp"
            android:ems="10"
            android:gravity="bottom|center"
            android:hint="請(qǐng)輸入用戶名"
            android:inputType="textPersonName"
            android:paddingLeft="20dp"
            android:textSize="24sp" />

        <EditText
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:layout_marginLeft="40dp"
            android:layout_marginTop="25dp"
            android:layout_marginRight="30dp"
            android:layout_marginBottom="20dp"
            android:ems="10"
            android:gravity="bottom|center"
            android:hint="請(qǐng)輸入密碼"
            android:inputType="textPassword"
            android:paddingLeft="20dp"
            android:textSize="24sp"
            android:visibility="visible" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <Button
            android:id="@+id/login"
            android:layout_width="320dp"
            android:layout_height="75dp"
            android:layout_gravity="bottom|center"
            android:layout_marginTop="30dp"
            android:text="登錄"
            android:textSize="24sp" />

        <Button
            android:id="@+id/register"
            android:layout_width="320dp"
            android:layout_height="75dp"
            android:layout_gravity="bottom|center"
            android:layout_marginTop="30dp"
            android:text="注冊(cè)"
            android:textSize="24sp" />
    </LinearLayout>
</LinearLayout>

LoginActivity.java

public class LoginActivity extends AppCompatActivity {
    private EditText name;
    private EditText password;

    Button login,register;  //登錄、注冊(cè)按鈕

    @Override
    protected void onCreate(Bundle savedState){
        super.onCreate(savedState);
        setContentView(R.layout.activity_login);

        name = findViewById(R.id.Username);  //獲取輸入的賬號(hào)
        password = findViewById(R.id.password);  //獲取輸入的密碼
        login = findViewById(R.id.login);
        register = findViewById(R.id.register);

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = "http://后端部署ip地址:8080/user/login";
                String username = name.getText().toString().trim();
                String userPassword = password.getText().toString().trim();

                if(username.equals("")){
                   Toast.makeText(LoginActivity.this, "請(qǐng)輸入用戶名", Toast.LENGTH_SHORT).show();

                }else if(userPassword.equals("")){
                   Toast.makeText(LoginActivity.this, "請(qǐng)輸入密碼", Toast.LENGTH_SHORT).show();

                }else {

                    //請(qǐng)求傳入的參數(shù)
                    JSONObject user = new JSONObject();
                    try{
                        user.put("userName",username);
                        user.put("userPassword",userPassword);
                    }catch (JSONException e){
                        e.printStackTrace();
                    }


                    OkHttpClient httpClient = new OkHttpClient();
                    MediaType type = MediaType.parse("application/json;charset=utf-8");
                    RequestBody requestBody = RequestBody.create(type,""+ user);

                    Request getRequest = new Request.Builder()
                        .url(url)
                        .post(requestBody)
                        .build();
                    Call call = httpClient.newCall(getRequest);
                    call.enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {

                            Log.i(TAG, "post請(qǐng)求失敗 \n" +
                                "*********請(qǐng)求體,傳送數(shù)據(jù)*********** \n"+
                                requestBody.toString() + "\n"+
                                "*****user里的數(shù)據(jù)***** \n"+
                                user);
                        }

                        @Override
                        public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                            assert response.body() != null;
                            String R = response.body().string();
                            Log.i(TAG, "okHttpPost enqueue: \n " +
                                "onResponse:"+ response.toString() +"\n " +
                                "body:" +R);
                            //將resoust轉(zhuǎn)換成jsonPath 格式
//                            io.restassured.path.json.JsonPath jsonPath =io.restassured.path.json.JsonPath.from(R);
                            try {
                                JSONObject toJsonObj= new JSONObject(R);
                                if(response.code()==200 && toJsonObj.get("code").equals(1)){
                                    Intent intent = new Intent();
                                    intent.setClass(LoginActivity.this, MainActivity.class);
                                    startActivity(intent);
                                }
                                else {
                                    Looper.prepare();
                                    Toast.makeText(LoginActivity.this, toJsonObj.get("code")+"****"+toJsonObj.get("msg").toString(), Toast.LENGTH_SHORT).show();
                                    Looper.loop();
                                }
                            } catch (JSONException e) {
                                throw new RuntimeException(e);
                            }

                        }
                    });

                }
            }
        });

       
    }


}

3.注意踩坑

(1)后端數(shù)據(jù)庫(kù)配置

建議后端代碼運(yùn)行時(shí),確認(rèn)數(shù)據(jù)庫(kù)是否能正常連接,不只看IDEA連接數(shù)據(jù)庫(kù),更要看application.yml配置文件里的數(shù)據(jù)庫(kù)

(2)Android傳數(shù)據(jù)時(shí)使用的URL

建議查看后端運(yùn)行的所在網(wǎng)絡(luò)IPV4地址,即為url請(qǐng)求地址。我在本機(jī)上運(yùn)行的后端,但是有時(shí)候電腦聯(lián)網(wǎng)不同,IPV4地址也不同,url會(huì)變,不一致則出現(xiàn)請(qǐng)求失敗的情況。

(3)Android okhttp3網(wǎng)絡(luò)請(qǐng)求框架

建議初學(xué)者仔細(xì)閱讀官方文檔和教程,不要在網(wǎng)上隨意找個(gè)代碼,大家的代碼思路不太相同,復(fù)制別人的代碼,但是又搞不懂請(qǐng)求流程,反而耗費(fèi)時(shí)間文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-517669.html

到了這里,關(guān)于Android前端+Spring Boot后端 登錄功能實(shí)現(xiàn)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 微信小程序的授權(quán)登錄-Java 后端 (Spring boot)

    微信開(kāi)發(fā)文檔鏈接:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/login.html 一個(gè)可以測(cè)試的微信小程序 此微信小程序的APPID和APPscret(至開(kāi)發(fā)者后臺(tái)獲?。?從時(shí)序圖我們可以了解到流程大致分為兩步: 小程序端獲取code后傳給Java后臺(tái) Java后臺(tái)獲取code后向微信后臺(tái)接口

    2024年02月09日
    瀏覽(97)
  • 網(wǎng)頁(yè)版Java(Spring/Spring Boot/Spring MVC)五子棋項(xiàng)目(二)前后端實(shí)現(xiàn)用戶的登錄和注冊(cè)功能【用戶模塊】

    網(wǎng)頁(yè)版Java(Spring/Spring Boot/Spring MVC)五子棋項(xiàng)目(二)前后端實(shí)現(xiàn)用戶的登錄和注冊(cè)功能【用戶模塊】

    1. 用戶id 2. 用戶名 3. 密碼 4. 天梯積分 5. 總場(chǎng)數(shù) 6. 獲勝場(chǎng)數(shù) 和數(shù)據(jù)庫(kù)的數(shù)據(jù)一一對(duì)應(yīng) 1. 連接數(shù)據(jù)庫(kù)(application.yml連接數(shù)據(jù)庫(kù)) 這個(gè)可以直接賦值代碼 2. 創(chuàng)建UserMapper的Java接口(interface UserMapper) 創(chuàng)建接口,在UserMapper.xml實(shí)現(xiàn) 在model中創(chuàng)建 需要實(shí)現(xiàn)@Mapper注釋 3. 創(chuàng)建UserMappe

    2024年02月13日
    瀏覽(23)
  • Spring Boot 統(tǒng)一功能處理(攔截器實(shí)現(xiàn)用戶登錄權(quán)限的統(tǒng)一校驗(yàn)、統(tǒng)一異常返回、統(tǒng)一數(shù)據(jù)格式返回)

    Spring Boot 統(tǒng)一功能處理(攔截器實(shí)現(xiàn)用戶登錄權(quán)限的統(tǒng)一校驗(yàn)、統(tǒng)一異常返回、統(tǒng)一數(shù)據(jù)格式返回)

    目錄 1. 用戶登錄權(quán)限校驗(yàn) 1.1 最初用戶登錄權(quán)限效驗(yàn) 1.2 Spring AOP 用戶統(tǒng)?登錄驗(yàn)證 1.3 Spring 攔截器 (1)創(chuàng)建自定義攔截器 (2)將自定義攔截器添加到系統(tǒng)配置中,并設(shè)置攔截的規(guī)則 1.4 練習(xí):登錄攔截器 (1)實(shí)現(xiàn) UserController 實(shí)體類(lèi) (2)返回的登錄頁(yè)面:login.html (3)實(shí)

    2024年02月12日
    瀏覽(58)
  • 【安卓app開(kāi)發(fā)一】Android Studio + Bmob后端云實(shí)現(xiàn)注冊(cè)&登錄賬號(hào)、密碼找回、意見(jiàn)反饋及數(shù)據(jù)可視化

    【安卓app開(kāi)發(fā)一】Android Studio + Bmob后端云實(shí)現(xiàn)注冊(cè)&登錄賬號(hào)、密碼找回、意見(jiàn)反饋及數(shù)據(jù)可視化

    目錄 前言 概覽 Bmob后端云介紹 Bmob后端云與Android Studio配置 一、Bmob后端云 ?二、Android Studio配置 工具類(lèi) 一、User類(lèi) 二、Suit類(lèi) 三、Code類(lèi) 實(shí)現(xiàn)類(lèi) ?一、登錄代碼 ?二、注冊(cè)代碼 ?三、找回密碼代碼 ?四、想法反饋代碼 ? ? ? ? 本項(xiàng)目尚在開(kāi)發(fā)階段,主要針對(duì)的是安卓用戶,通

    2024年02月08日
    瀏覽(26)
  • Android Studio實(shí)現(xiàn)Mysql(5.7)數(shù)據(jù)庫(kù)增刪改查(上)——用戶登錄功能實(shí)現(xiàn)

    Android Studio實(shí)現(xiàn)Mysql(5.7)數(shù)據(jù)庫(kù)增刪改查(上)——用戶登錄功能實(shí)現(xiàn)

    前言:如果android studio安裝和第一次跑helloworld有什么問(wèn)題可以參考:(34條消息) Android Studio踩的那些坑(gradle build、unable to find tasks to···等等)_TidesWise的博客-CSDN博客 目錄 準(zhǔn)備工作 查詢學(xué)生數(shù)目demo:? 增刪改查完整案例 1、在Android Studio中新建項(xiàng)目My Application; 2、在Androi

    2024年02月10日
    瀏覽(19)
  • React.js前端 + Spring Boot后端員工管理

    該項(xiàng)目是一個(gè)員工管理系統(tǒng),前端使用 React.js 構(gòu)建,后端使用 Spring Boot 和 Data JPA 和 Lombok 構(gòu)建。它提供了有效管理員工信息的全面解決方案。 特征 響應(yīng)式設(shè)計(jì):響應(yīng)式 UI 設(shè)計(jì),確保跨各種設(shè)備的可用性。 數(shù)據(jù)驗(yàn)證:驗(yàn)證用戶輸入以確保數(shù)據(jù)完整性。 使用的技術(shù) 前端:R

    2024年04月28日
    瀏覽(24)
  • Spring Boot后端+Vue前端:打造高效二手車(chē)交易系統(tǒng)

    Spring Boot后端+Vue前端:打造高效二手車(chē)交易系統(tǒng)

    作者介紹: ??大廠全棧碼農(nóng)|畢設(shè)實(shí)戰(zhàn)開(kāi)發(fā),專(zhuān)注于大學(xué)生項(xiàng)目實(shí)戰(zhàn)開(kāi)發(fā)、講解和畢業(yè)答疑輔導(dǎo)。 ?? 獲取源碼聯(lián)系方式請(qǐng)查看文末 ?? ?推薦訂閱精彩專(zhuān)欄 ???? 避免錯(cuò)過(guò)下次更新 Springboot項(xiàng)目精選實(shí)戰(zhàn)案例 更多項(xiàng)目: CSDN主頁(yè)YAML墨韻 學(xué)如逆水行舟,不進(jìn)則退。學(xué)習(xí)如趕

    2024年04月28日
    瀏覽(23)
  • Spring Boot后端與Vue前端融合:構(gòu)建高效旅游管理系統(tǒng)

    Spring Boot后端與Vue前端融合:構(gòu)建高效旅游管理系統(tǒng)

    作者介紹: ??大廠全棧碼農(nóng)|畢設(shè)實(shí)戰(zhàn)開(kāi)發(fā),專(zhuān)注于大學(xué)生項(xiàng)目實(shí)戰(zhàn)開(kāi)發(fā)、講解和畢業(yè)答疑輔導(dǎo)。 ?? 獲取源碼聯(lián)系方式請(qǐng)查看文末 ?? ?推薦訂閱精彩專(zhuān)欄 ???? 避免錯(cuò)過(guò)下次更新 Springboot項(xiàng)目精選實(shí)戰(zhàn)案例 更多項(xiàng)目: CSDN主頁(yè)YAML墨韻 學(xué)如逆水行舟,不進(jìn)則退。學(xué)習(xí)如趕

    2024年04月28日
    瀏覽(26)
  • 手把手教你實(shí)現(xiàn):Android注冊(cè)登錄功能,通過(guò)本地服務(wù)器保存用戶賬號(hào)密碼到數(shù)據(jù)庫(kù)

    手把手教你實(shí)現(xiàn):Android注冊(cè)登錄功能,通過(guò)本地服務(wù)器保存用戶賬號(hào)密碼到數(shù)據(jù)庫(kù)

    代碼我放到文章最后面了 首先你需要電腦一臺(tái):如果沒(méi)有電腦將會(huì)很難辦呢 -----沃茲基碩德 下載并安裝以下開(kāi)發(fā)工具 Android Studio 官網(wǎng)最新版 用來(lái)開(kāi)發(fā) 安卓App IntelliJ IDEA 官網(wǎng)最新版 用來(lái)開(kāi)發(fā) 后端 ,處理安卓APP的請(qǐng)求 Navicat for MySql 官網(wǎng)最新版 數(shù)據(jù)庫(kù)可視化工具,用來(lái)查看數(shù)

    2024年01月16日
    瀏覽(30)
  • SpringBoot + Vue前后端分離項(xiàng)目實(shí)戰(zhàn) || 三:Spring Boot后端與Vue前端連接

    SpringBoot + Vue前后端分離項(xiàng)目實(shí)戰(zhàn) || 三:Spring Boot后端與Vue前端連接

    系列文章: SpringBoot + Vue前后端分離項(xiàng)目實(shí)戰(zhàn) || 一:Vue前端設(shè)計(jì) SpringBoot + Vue前后端分離項(xiàng)目實(shí)戰(zhàn) || 二:Spring Boot后端與數(shù)據(jù)庫(kù)連接 SpringBoot + Vue前后端分離項(xiàng)目實(shí)戰(zhàn) || 三:Spring Boot后端與Vue前端連接 SpringBoot + Vue前后端分離項(xiàng)目實(shí)戰(zhàn) || 四:用戶管理功能實(shí)現(xiàn) SpringBoot + Vue前后

    2024年02月12日
    瀏覽(46)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包