Kotlin Android Web服务-Volley库注册,登录和注销教程

在本教程中, 我们将使用Volley库和JSON创建基本的用户注册和登录模块。 Volley是一个HTTP库, 它为我们的应用程序提供了网络连接功能。
使用PHP和MySQL创建创建注册和登录的Web API。在实施客户端Android应用程序代码之前, 请先在https://www.srcmini.com/android-volley-library-registration-login-logout查看服务器端API代码。
在Android应用程序中, 我们将创建用于注册用户, 登录用户的三个活动类, 并在主要活动中(作为个人资料)显示用户详细信息。
在布局中创建一个activity_main.xml并添加以下代码。此活动用于显示用户配置文件的详细信息。
activity_main.xml

< ?xml version="1.0" encoding="utf-8"?> < android.support.constraint.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="example.srcmini.com.kotlinvolleyregistrationloginsystem.MainActivity"> < LinearLayoutandroid:layout_width="match_parent"android:layout_height="fill_parent"android:layout_marginBottom="8dp"android:layout_marginEnd="8dp"android:layout_marginStart="8dp"android:layout_marginTop="8dp"android:orientation="vertical"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintHorizontal_bias="0.0"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"app:layout_constraintVertical_bias="0.0"> < TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:gravity="center"android:text="Welcome to Profile"android:textAppearance="@style/Base.TextAppearance.AppCompat.Large" /> < TableLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="50dp"> < TableRow> < TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:padding="10dp"android:text="Id"android:textAppearance="@style/Base.TextAppearance.AppCompat.Large" /> < TextViewandroid:id="@+id/textViewId"android:layout_width="wrap_content"android:layout_height="wrap_content"android:padding="10dp"android:text="id"android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium" /> < /TableRow> < TableRow> < TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:padding="10dp"android:text="Username"android:textAppearance="@style/Base.TextAppearance.AppCompat.Large" /> < TextViewandroid:id="@+id/textViewUsername"android:layout_width="wrap_content"android:layout_height="wrap_content"android:padding="10dp"android:text="username"android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium" /> < /TableRow> < TableRow> < TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:padding="10dp"android:text="Email"android:textAppearance="@style/Base.TextAppearance.AppCompat.Large" /> < TextViewandroid:id="@+id/textViewEmail"android:layout_width="wrap_content"android:layout_height="wrap_content"android:padding="10dp"android:text="useremail"android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium" /> < /TableRow> < TableRow> < TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:padding="10dp"android:text="Gender"android:textAppearance="@style/Base.TextAppearance.AppCompat.Large" /> < TextViewandroid:id="@+id/textViewGender"android:layout_width="wrap_content"android:layout_height="wrap_content"android:padding="10dp"android:text="gender"android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium" /> < /TableRow> < /TableLayout> < Buttonandroid:id="@+id/buttonLogout"android:layout_width="210dp"android:layout_height="50dp"android:layout_marginTop="150dp"android:layout_marginLeft="75dp"android:text="Logout" /> < /LinearLayout> < /android.support.constraint.ConstraintLayout>

现在, 使用以下代码在布局目录中创建一个activity_login.xml文件。此活动用于用户登录UI。 activity_login.xml
< ?xml version="1.0" encoding="utf-8"?> < android.support.constraint.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="example.srcmini.com.kotlinvolleyregistrationloginsystem.LoginActivity"> < TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginEnd="8dp"android:layout_marginStart="8dp"android:layout_marginTop="16dp"android:gravity="center"android:text="Login"android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintHorizontal_bias="1.0"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /> < EditTextandroid:id="@+id/etUserName"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginBottom="8dp"android:layout_marginEnd="8dp"android:layout_marginStart="8dp"android:layout_marginTop="8dp"android:ems="10"android:inputType="textPersonName"android:hint="user name"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintHorizontal_bias="0.0"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"app:layout_constraintVertical_bias="0.144" /> < EditTextandroid:id="@+id/etUserPassword"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginEnd="8dp"android:layout_marginStart="8dp"android:layout_marginTop="8dp"android:hint="password"android:ems="10"android:inputType="textPassword"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/etUserName" /> < Buttonandroid:id="@+id/btnLogin"android:layout_width="210dp"android:layout_height="50dp"android:layout_marginBottom="8dp"android:layout_marginEnd="8dp"android:layout_marginStart="8dp"android:layout_marginTop="8dp"android:text="Login"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/etUserName"app:layout_constraintVertical_bias="0.754" /> < TextViewandroid:id="@+id/tvRegister"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginBottom="8dp"android:layout_marginEnd="8dp"android:layout_marginStart="8dp"android:layout_marginTop="8dp"android:gravity="center"android:text="Create New Account\n Register Here"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/btnLogin"app:layout_constraintVertical_bias="0.405" /> < ProgressBarandroid:id="@+id/progressBar"android:visibility="gone"style="?android:attr/progressBarStyle"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginBottom="8dp"android:layout_marginEnd="8dp"android:layout_marginStart="8dp"android:layout_marginTop="8dp"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.456" /> < /android.support.constraint.ConstraintLayout>

使用以下代码在布局目录中创建一个activity_register.xml文件。此活动用于用户注册UI。
activity_register.xml
< ?xml version="1.0" encoding="utf-8"?> < android.support.constraint.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="example.srcmini.com.kotlinvolleyregistrationloginsystem.RegisterActivity"> < LinearLayoutandroid:layout_width="match_parent"android:layout_height="519dp"android:layout_centerVertical="true"android:layout_marginBottom="8dp"android:layout_marginEnd="8dp"android:layout_marginStart="8dp"android:layout_marginTop="8dp"android:orientation="vertical"android:padding="10dp"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.0"> < TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="Register"android:gravity="center"android:textAppearance="@style/Base.TextAppearance.AppCompat.Large" /> < EditTextandroid:id="@+id/editTextUsername"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginBottom="8dp"android:layout_marginTop="20dp"android:hint="Username"android:inputType="text" /> < EditTextandroid:id="@+id/editTextEmail"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginBottom="8dp"android:layout_marginTop="8dp"android:hint="Email"android:inputType="textEmailAddress" /> < EditTextandroid:id="@+id/editTextPassword"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginBottom="8dp"android:layout_marginTop="8dp"android:fontFamily="sans-serif"android:hint="Password"android:inputType="textPassword" /> < RadioGroupandroid:id="@+id/radioGender"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginBottom="8dp"android:layout_marginTop="8dp"android:orientation="horizontal"> < RadioButtonandroid:id="@+id/radioButtonMale"android:layout_width="wrap_content"android:layout_height="wrap_content"android:checked="true"android:text="Male" /> < RadioButtonandroid:id="@+id/radioButtonFemale"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Female" /> < /RadioGroup> < Buttonandroid:id="@+id/buttonRegister"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginBottom="8dp"android:layout_marginTop="90dp"android:text="Register" /> < TextViewandroid:id="@+id/textViewLogin"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginBottom="8dp"android:layout_marginTop="8dp"android:text="Already Registered?\nLogin Here"android:textAlignment="center"android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium" /> < /LinearLayout> < ProgressBarandroid:visibility="gone"android:id="@+id/progressBar"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:layout_centerVertical="true" /> < /android.support.constraint.ConstraintLayout>

在build.gradle文件中添加volley库依赖项。
build.gradle
dependencies {implementation fileTree(dir: 'libs', include: ['*.jar'])implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"implementation 'com.android.support:appcompat-v7:26.1.0'implementation 'com.android.support.constraint:constraint-layout:1.1.3'testImplementation 'junit:junit:4.12'implementation 'com.android.volley:volley:1.0.0'}

使用以下代码创建一个名为User.kt的数据模型类。
User.kt
package example.srcmini.com.kotlinvolleyregistrationloginsystemclass User(var id: Int, var name: String?, var email: String?, var gender: String?)

我们需要定义调用服务器端API的URL。
创建一个URLs.kt类并定义URL。
URLs.kt
package example.srcmini.com.kotlinvolleyregistrationloginsystemobject URLs {private val ROOT_URL = "http://192.168.1.35/androidphpmysql/registrationapi.php?apicall="val URL_REGISTER = ROOT_URL + "signup"val URL_LOGIN = ROOT_URL + "login"}

VolleySingleton.kt
package example.srcmini.com.kotlinvolleyregistrationloginsystemimport android.content.Contextimport com.android.volley.Requestimport com.android.volley.RequestQueueimport com.android.volley.toolbox.Volleyclass VolleySingleton private constructor(context: Context) {private var mRequestQueue: RequestQueue// applicationContext is key, it keeps you from leaking the// Activity or BroadcastReceiver if someone passes one in.val requestQueue: RequestQueueget() {if (mRequestQueue == null) {mRequestQueue = Volley.newRequestQueue(mCtx?.applicationContext)}return mRequestQueue}init {mCtx = contextmRequestQueue = requestQueue}fun < T> addToRequestQueue(req: Request< T> ) {requestQueue.add(req)}companion object {private var mInstance: VolleySingleton? = nullprivate var mCtx: Context? = null@Synchronizedfun getInstance(context: Context): VolleySingleton {if (mInstance == null) {mInstance = VolleySingleton(context)}return mInstance as VolleySingleton}}}

创建一个名为SharedPreferences.kt的类。在此类中, 我们使用SharedPreferences类存储用户详细信息。 SharedPreferences类包含具有以下功能的四个方法:
  • userLogin():此函数用于在登录后将用户信息存储在SharedPreferences中。
  • isLoggedIn:此方法检查用户是否已经登录。
  • user:User get():如果登录, 此方法获取用户信息。
  • logout():此函数清除SharedPreferences数据并注销用户。
SharedPrefManager.kt
package example.srcmini.com.kotlinvolleyregistrationloginsystemimport android.content.Contextimport android.content.Intentclass SharedPrefManager private constructor(context: Context) {//this method will checker whether user is already logged in or notval isLoggedIn: Booleanget() {val sharedPreferences = ctx?.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE)return sharedPreferences?.getString(KEY_USERNAME, null) != null}//this method will give the logged in userval user: Userget() {val sharedPreferences = ctx?.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE)return User(sharedPreferences!!.getInt(KEY_ID, -1), sharedPreferences.getString(KEY_USERNAME, null), sharedPreferences.getString(KEY_EMAIL, null), sharedPreferences.getString(KEY_GENDER, null))}init {ctx = context}//this method will store the user data in shared preferencesfun userLogin(user: User) {val sharedPreferences = ctx?.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE)val editor = sharedPreferences?.edit()editor?.putInt(KEY_ID, user.id)editor?.putString(KEY_USERNAME, user.name)editor?.putString(KEY_EMAIL, user.email)editor?.putString(KEY_GENDER, user.gender)editor?.apply()}//this method will logout the userfun logout() {val sharedPreferences = ctx?.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE)val editor = sharedPreferences?.edit()editor?.clear()editor?.apply()ctx?.startActivity(Intent(ctx, LoginActivity::class.java))}companion object {private val SHARED_PREF_NAME = "volleyregisterlogin"private val KEY_USERNAME = "keyusername"private val KEY_EMAIL = "keyemail"private val KEY_GENDER = "keygender"private val KEY_ID = "keyid"private var mInstance: SharedPrefManager? = nullprivate var ctx: Context? = null@Synchronizedfun getInstance(context: Context): SharedPrefManager {if (mInstance == null) {mInstance = SharedPrefManager(context)}return mInstance as SharedPrefManager}}}

现在, 在MainActivity.kt类中, 如果用户已登录, 我们将显示用户信息, 否则, 它将重定向到LoginActivity.kt类。当单击按钮时, 使用onClick()方法注销用户。
MainActivity.kt
package example.srcmini.com.kotlinvolleyregistrationloginsystemimport android.support.v7.app.AppCompatActivityimport android.os.Bundleimport android.content.Intentimport android.view.Viewimport android.widget.Buttonimport android.widget.TextViewclass MainActivity : AppCompatActivity(), View.OnClickListener {internal lateinit var id: TextViewinternal lateinit var userName: TextViewinternal lateinit var userEmail: TextViewinternal lateinit var gender: TextViewinternal lateinit var btnLogout: Buttonoverride fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)if (SharedPrefManager.getInstance(this).isLoggedIn) {id = findViewById(R.id.textViewId)userName = findViewById(R.id.textViewUsername)userEmail = findViewById(R.id.textViewEmail)gender = findViewById(R.id.textViewGender)btnLogout = findViewById(R.id.buttonLogout)val user = SharedPrefManager.getInstance(this).userid.text = user.id.toString()userEmail.text = user.emailgender.text = user.genderuserName.text = user.namebtnLogout.setOnClickListener(this)} else {val intent = Intent(this@MainActivity, LoginActivity::class.java)startActivity(intent)finish()}}override fun onClick(view: View) {if (view == btnLogout) {SharedPrefManager.getInstance(applicationContext).logout()}}}

在LoginActivity.kt类中, 我们检查用户是否已经登录, 如果为true, 则重定向到MainActivity.kt类, 否则, 允许用户登录。
网络模块使用Volley库的StringRequest类。 StringRequest类的对象采用类型请求方法, URL和响应的参数。
LoginActivity.kt
package example.srcmini.com.kotlinvolleyregistrationloginsystemimport android.support.v7.app.AppCompatActivityimport android.os.Bundleimport android.content.Intentimport android.text.TextUtilsimport android.view.Viewimport android.widget.EditTextimport android.widget.ProgressBarimport android.widget.Toastimport com.android.volley.AuthFailureErrorimport com.android.volley.Requestimport com.android.volley.Responseimport com.android.volley.toolbox.StringRequestimport kotlinx.android.synthetic.main.activity_login.*import org.json.JSONExceptionimport org.json.JSONObjectimport java.util.HashMapclass LoginActivity : AppCompatActivity() {internal lateinit var etName: EditTextinternal lateinit var etPassword: EditTextinternal lateinit var progressBar: ProgressBaroverride fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_login)if (SharedPrefManager.getInstance(this).isLoggedIn) {finish()startActivity(Intent(this, MainActivity::class.java))}progressBar = findViewById(R.id.progressBar)etName = findViewById(R.id.etUserName)etPassword = findViewById(R.id.etUserPassword)//calling the method userLogin() for login the userbtnLogin.setOnClickListener(View.OnClickListener {userLogin()})//if user presses on textview it call the activity RegisterActivitytvRegister.setOnClickListener(View.OnClickListener {finish()startActivity(Intent(applicationContext, RegisterActivity::class.java))})}private fun userLogin() {//first getting the valuesval username = etName.text.toString()val password = etPassword.text.toString()//validating inputsif (TextUtils.isEmpty(username)) {etName.error = "Please enter your username"etName.requestFocus()return}if (TextUtils.isEmpty(password)) {etPassword.error = "Please enter your password"etPassword.requestFocus()return}//if everything is fineval stringRequest = object : StringRequest(Request.Method.POST, URLs.URL_LOGIN, Response.Listener { response -> progressBar.visibility = View.GONEtry {//converting response to json objectval obj = JSONObject(response)//if no error in responseif (!obj.getBoolean("error")) {Toast.makeText(applicationContext, obj.getString("message"), Toast.LENGTH_SHORT).show()//getting the user from the responseval userJson = obj.getJSONObject("user")//creating a new user objectval user = User(userJson.getInt("id"), userJson.getString("username"), userJson.getString("email"), userJson.getString("gender"))//storing the user in shared preferencesSharedPrefManager.getInstance(applicationContext).userLogin(user)//starting the MainActivityfinish()startActivity(Intent(applicationContext, MainActivity::class.java))} else {Toast.makeText(applicationContext, obj.getString("message"), Toast.LENGTH_SHORT).show()}} catch (e: JSONException) {e.printStackTrace()}}, Response.ErrorListener { error -> Toast.makeText(applicationContext, error.message, Toast.LENGTH_SHORT).show() }) {@Throws(AuthFailureError::class)override fun getParams(): Map< String, String> {val params = HashMap< String, String> ()params["username"] = usernameparams["password"] = passwordreturn params}}VolleySingleton.getInstance(this).addToRequestQueue(stringRequest)}}

RegisterActivity.kt类用于注册用户。此类最初检查用户登录, 如果为true, 则重定向到MainActivity.kt类, 否则, 允许用户注册。
与LoginActivity.kt类类似, 我们使用Volley库的StringRequest类进行网络连接, 并传递类型请求方法, URL和响应的参数。 Response.Listener处理服务器生成的响应。
RegisterActivity.kt
package example.srcmini.com.kotlinvolleyregistrationloginsystemimport android.support.v7.app.AppCompatActivityimport android.os.Bundleimport android.content.Intentimport android.text.TextUtilsimport android.view.Viewimport android.widget.EditTextimport android.widget.ProgressBarimport android.widget.RadioButtonimport android.widget.RadioGroupimport android.widget.Toastimport com.android.volley.AuthFailureErrorimport com.android.volley.Requestimport com.android.volley.Responseimport com.android.volley.toolbox.StringRequestimport kotlinx.android.synthetic.main.activity_register.*import org.json.JSONExceptionimport org.json.JSONObjectimport java.util.HashMapclass RegisterActivity : AppCompatActivity() {internal lateinit var editTextUsername: EditTextinternal lateinit var editTextEmail: EditTextinternal lateinit var editTextPassword: EditTextinternal lateinit var radioGroupGender: RadioGroupinternal lateinit var progressBar: ProgressBaroverride fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_register)progressBar = findViewById(R.id.progressBar)//if the user is already logged in we will directly start the MainActivity (profile) activityif (SharedPrefManager.getInstance(this).isLoggedIn) {finish()startActivity(Intent(this, MainActivity::class.java))return}editTextUsername = findViewById(R.id.editTextUsername)editTextEmail = findViewById(R.id.editTextEmail)editTextPassword = findViewById(R.id.editTextPassword)radioGroupGender = findViewById(R.id.radioGender)buttonRegister.setOnClickListener(View.OnClickListener {//if user pressed on button register//here we will register the user to serverregisterUser()})textViewLogin.setOnClickListener(View.OnClickListener {finish()startActivity(Intent(this@RegisterActivity, LoginActivity::class.java))})}private fun registerUser() {val username = editTextUsername.text.toString().trim { it < = ' ' }val email = editTextEmail.text.toString().trim { it < = ' ' }val password = editTextPassword.text.toString().trim { it < = ' ' }val gender = (findViewById< View> (radioGroupGender.checkedRadioButtonId) as RadioButton).text.toString()//first we will do the validationsif (TextUtils.isEmpty(username)) {editTextUsername.error = "Please enter username"editTextUsername.requestFocus()return}if (TextUtils.isEmpty(email)) {editTextEmail.error = "Please enter your email"editTextEmail.requestFocus()return}if (!android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {editTextEmail.error = "Enter a valid email"editTextEmail.requestFocus()return}if (TextUtils.isEmpty(password)) {editTextPassword.error = "Enter a password"editTextPassword.requestFocus()return}val stringRequest = object : StringRequest(Request.Method.POST, URLs.URL_REGISTER, Response.Listener { response -> progressBar.visibility = View.GONEtry {//converting response to json objectval obj = JSONObject(response)//if no error in responseif (!obj.getBoolean("error")) {Toast.makeText(applicationContext, obj.getString("message"), Toast.LENGTH_SHORT).show()//getting the user from the responseval userJson = obj.getJSONObject("user")//creating a new user objectval user = User(userJson.getInt("id"), userJson.getString("username"), userJson.getString("email"), userJson.getString("gender"))//storing the user in shared preferencesSharedPrefManager.getInstance(applicationContext).userLogin(user)//starting the MainActivity activityfinish()startActivity(Intent(applicationContext, MainActivity::class.java))} else {Toast.makeText(applicationContext, obj.getString("message"), Toast.LENGTH_SHORT).show()}} catch (e: JSONException) {e.printStackTrace()}}, Response.ErrorListener { error -> Toast.makeText(applicationContext, error.message, Toast.LENGTH_SHORT).show() }) {@Throws(AuthFailureError::class)override fun getParams(): Map< String, String> {val params = HashMap< String, String> ()params["username"] = usernameparams["email"] = emailparams["password"] = passwordparams["gender"] = genderreturn params}}VolleySingleton.getInstance(this).addToRequestQueue(stringRequest)}}

AndroidManifest.xml
在AndroidManifest.xml文件中添加以下权限
< uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> < uses-permission android:name="android.permission.INTERNET" />

注意:服务器端的注册和登录代码是通过MySQL和MySQL在https://www.srcmini.com/android-volley-library-registration-login-logout上实现的。 【Kotlin Android Web服务-Volley库注册,登录和注销教程】输出:
Kotlin Android Web服务-Volley库注册,登录和注销教程

文章图片
Kotlin Android Web服务-Volley库注册,登录和注销教程

文章图片
Kotlin Android Web服务-Volley库注册,登录和注销教程

文章图片
Kotlin Android Web服务-Volley库注册,登录和注销教程

文章图片
Kotlin Android Web服务-Volley库注册,登录和注销教程

文章图片
Kotlin Android Web服务-Volley库注册,登录和注销教程

文章图片
Kotlin Android Web服务-Volley库注册,登录和注销教程

文章图片
Kotlin Android Web服务-Volley库注册,登录和注销教程

文章图片
Kotlin Android Web服务-Volley库注册,登录和注销教程

文章图片
Kotlin Android Web服务-Volley库注册,登录和注销教程

文章图片

    推荐阅读