im trying to do login using retrofit and viewmodel
i have done successfully login with only retrofit...referred this tutorial--> https://www.youtube.com/watch?v=j0wH0m_xYLs
i havent found any tutorial related to login using viewmodel
found this stackoverflow question but it is still unanswered --> How to make retrofit API call request method post using LiveData and ViewModel
here is my call activity:--
class LoginActivity : BaseClassActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.login_activity)
val button = findViewById<ImageView>(R.id.plusbutton)
val forgotpassword=findViewById<TextView>(R.id.forgotpassword)
button.setOnClickListener {
val i = Intent(applicationContext, RegisterActivity::class.java)
startActivity(i)
}
forgotpassword.setOnClickListener{
val i = Intent(applicationContext, ForgotPassword::class.java)
startActivity(i)
}
loginbtn.setOnClickListener {
val email = loginuser.text.toString().trim()
val password = loginpassword.text.toString().trim()
if (email.isEmpty()) {
Toast.makeText(
applicationContext, "Data is missing",Toast.LENGTH_LONG
).show()
loginuser.error = "Email required"
loginuser.requestFocus()
return@setOnClickListener
}
if (password.isEmpty()) {
loginpassword.error = "Password required"
loginpassword.requestFocus()
return@setOnClickListener
}
RetrofitClient.instance.userLogin(email, password)
.enqueue(object : Callback<LoginResponse> {
override fun onFailure(call: Call<LoginResponse>, t: Throwable) {
Log.d("res", "" + t)
}
override fun onResponse(
call: Call<LoginResponse>,
response: Response<LoginResponse>
) {
var res = response
Log.d("response check ", "" + response.body()?.status.toString())
if (res.body()?.status==200) {
SharedPrefManager.getInstance(applicationContext)
.saveUser(response.body()?.data!!)
val intent = Intent(applicationContext, HomeActivity::class.java)
intent.flags =
Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
showToast(applicationContext,res.body()?.message)
Log.d("kjsfgxhufb",response.body()?.status.toString())
startActivity(intent)
finish()
}
else
{
try {
val jObjError =
JSONObject(response.errorBody()!!.string())
showToast(applicationContext,jObjError.getString("user_msg"))
} catch (e: Exception) {
showToast(applicationContext,e.message)
Log.e("errorrr",e.message)
}
}
}
})
}
}}
Following is LoginResponse:-
data class LoginResponse(val status: Int, val data: Data, val message: String, val user_msg:String)
Data class:-
data class Data(
@SerializedName("id") val id: Int,
@SerializedName("role_id") val role_id: Int,
@SerializedName("first_name") val first_name: String?,
@SerializedName("last_name") val last_name: String?,
@SerializedName("email") val email: String?,
@SerializedName("username") val username: String?,
@SerializedName("profile_pic") val profile_pic: String?,
@SerializedName("country_id") val country_id: String?,
@SerializedName("gender") val gender: String?,
@SerializedName("phone_no") val phone_no: String,
@SerializedName("dob") val dob: String?,
@SerializedName("is_active") val is_active: Boolean,
@SerializedName("created") val created: String?,
@SerializedName("modified") val modified: String?,
@SerializedName("access_token") val access_token: String?
)
really need help desparately related to viewmodel for login
thanks in advance
Errors encountered while adding epicpandaforce answer:--
in loginviewmodel:--
in loginactivity:- 1-->
2-->
3-->