Registering a new profile 

Now we will create a function called registerUser(), which will help you to send requests to the server and fetch the output from the server. We will show you how to use RxJava in Chapter 8, Reactive Programming, and Retrofit in Chapter 4, Spring Modules for Android. Here is the code for registerUser():

private fun registerUser(){
val newProfile = Profile(null,
username_input_reg.text.toString(),
password_input_reg.text.toString(),
email_input_reg.text.toString(),
null,
first_name_input_reg.text.toString(),
last_name_input_reg.text.toString(),
contact_input_reg.text.toString(),
country_input_reg.text.toString())

APIClient.profileAPICall("","")
.registerProfile(newProfile)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
newUser ->
if(newUser.duplicate != null){
Toast.makeText(applicationContext,newUser.duplicate!!, Toast.LENGTH_SHORT).show()
}else {
PrefUtils.storeUsernameID(this, 1)
PrefUtils.storeUsername(this, username)
PrefUtils.storePassword(this, password)
val intent = Intent(this, LoginActivity::class.java)
startActivity(intent)
}


},{
error ->
Toast.makeText(applicationContext,error.message.toString(), Toast.LENGTH_SHORT).show()


})
}

Here, we will take the contents from EditText and create a Profile object. Then we take an observer that will fetch the profile list as JSON type and handle the updated list in the subscribe() function. If the result is complete, it will return in the first parameter, and then we will save the username, password, and userID locally using SharedPreferences and return to LoginActivity. If it throws an error, it will go to the second parameter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.7.102