Android使用OKhttp3实现登录注册功能+springboot搭建后端

Posted hequnwang10

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android使用OKhttp3实现登录注册功能+springboot搭建后端相关的知识,希望对你有一定的参考价值。

这篇教程主要实现android使用OKhttp3实现登录注册的功能,后端使用SSM框架。

一、Android前端实现

新建一个login的项目,主要的几个文件在这里

1、gradle引入OKhttp3依赖

    implementation 'com.squareup.okhttp3:okhttp:3.14.7'
    implementation 'com.squareup.okio:okio:1.17.5'

2、activity_main.xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">
    <EditText
        android:id="@+id/username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="用户名"
        android:textSize="34sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.051" />
    <EditText
        android:id="@+id/password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="密码"
        android:textSize="34sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/username"
        app:layout_constraintVertical_bias="0.067" />
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="register"
        android:text="注册"
        android:textSize="24sp"
        app:backgroundTint="#E91E63"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.501"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/password"
        app:layout_constraintVertical_bias="0.058" />
    <Button
        android:id="@+id/getUser"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="getUser"
        android:text="获取"
        android:textSize="24sp"
        app:backgroundTint="#E91E63"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.501"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button3"
        app:layout_constraintVertical_bias="0.174" />
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登录"
        android:textSize="24sp"
        app:backgroundTint="#E91E63"
        android:onClick="login"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.501"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button"
        app:layout_constraintVertical_bias="0.113" />
</androidx.constraintlayout.widget.ConstraintLayout>

3、AndroidManifest.xml配置文件

这里需要加上网络请求权限,添加网络权限的时候注意:在res目录下新建xml目录,创建network_security_config.xml文件

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true" />
</network-security-config>

并在配置文件中加入这行代码:

 android:networkSecurityConfig="@xml/network_security_config"

完整的AndroidManifest.xml文件如下

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.login">

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:networkSecurityConfig="@xml/network_security_config"
        android:supportsRtl="true"
        android:theme="@style/Theme.Login">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

4、创建User实体类

新建entity目录创建User实体类。注意这里我是根据后端数据库的字段创建的,在实现登录注册的时候只需要id,username,password 几个基础的字段就行,这里我比较多,读者可以自行忽略。
User.java

package com.example.login.entity;
public class User {
    private Integer userId;
    private String userName;
    private String userPassword;
    private String currentVersion;
    private String latestVersion;
    private String updateDescription;
    private String headPortrait;
    private String nickName;
    private String vipTime;
    private String userCategory;
    private String registerDate;

    public User(Integer userId, String userName, String userPassword, String currentVersion, String latestVersion, String updateDescription, String headPortrait, String nickName, String vipTime, String userCategory, String registerDate) {
        this.userId = userId;
        this.userName = userName;
        this.userPassword = userPassword;
        this.currentVersion = currentVersion;
        this.latestVersion = latestVersion;
        this.updateDescription = updateDescription;
        this.headPortrait = headPortrait;
        this.nickName = nickName;
        this.vipTime = vipTime;
        this.userCategory = userCategory;
        this.registerDate = registerDate;
    }

    public User() {
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserPassword() {
        return userPassword;
    }

    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword;
    }

    public String getCurrentVersion() {
        return currentVersion;
    }

    public void setCurrentVersion(String currentVersion) {
        this.currentVersion = currentVersion;
    }

    public String getLatestVersion() {
        return latestVersion;
    }

    public void setLatestVersion(String latestVersion) {
        this.latestVersion = latestVersion;
    }

    public String getUpdateDescription() {
        return updateDescription;
    }

    public void setUpdateDescription(String updateDescription) {
        this.updateDescription = updateDescription;
    }

    public String getHeadPortrait() {
        return headPortrait;
    }

    public void setHeadPortrait(String headPortrait) {
        this.headPortrait = headPortrait;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public String getVipTime() {
        return vipTime;
    }

    public void setVipTime(String vipTime) {
        this.vipTime = vipTime;
    }

    public String getUserCategory() {
        return userCategory;
    }

    public void setUserCategory(String userCategory) {
        this.userCategory = userCategory;
    }

    public String getRegisterDate() {
        return registerDate;
    }

    public void setRegisterDate(String registerDate) {
        this.registerDate = registerDate;
    }
}

5、MainActivity.java

package com.example.login;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.login.entity.User;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
    private EditText username;
    private EditText password;
    private User user;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        username = findViewById(R.id.username);
        password = findViewById(R.id.password);



    }

    public void register(View view) {
        user = new User();
        user.setUserName(username.getText().toString());
        user.setUserPassword(password.getText().toString());
        Log.d("whqusername",username.getText().toString());
        Log.d("whqpassword",password.getText().toString());
        new Thread(new Runnable() {
            @Override
            public void run() {
                MediaType JSON = MediaType.parse("application/json;charset=utf-8");
                JSONObject jsonObject = new JSONObject();
                OkHttpClient httpClient = new OkHttpClient();
                try {
                    jsonObject.put("userId",10);
                    jsonObject.put("userName",user.getUserName());
                    jsonObject.put("userPassword",user.getUserPassword());
                    jsonObject.put("currentVersion",null);
                    jsonObject.put("latestVersion",null);
                    jsonObject.put("updateDescription",null);
                    jsonObject.put("headPortrait",null);
                    jsonObject.put("nickName",user.getUserName());
                    jsonObject.put("vipTime",null);
                    jsonObject.put("userCategory",null);
                    jsonObject.put("registerDate",null);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                RequestBody requestBody = RequestBody.create(JSON, String.valueOf(jsonObject));
                String url = "http://ip:8001/server/user/addUser/";
                Request request = new Request.Builder()
                        .url(url)
                        .post以上是关于Android使用OKhttp3实现登录注册功能+springboot搭建后端的主要内容,如果未能解决你的问题,请参考以下文章

Android网络框架OKhttp3学习笔记

android 仿微信demo————登录功能实现(移动端)

Android Studio-基于SQLLITE实现登录注册功能

Android客户端实现注册/登录详解

android-servlet-mysql实现登录注册功能

android 仿微信demo————注册功能实现(移动端)