我正在尝试使用Retrofit和ViewModel进行登录。
我已经成功地使用仅Retrofit进行了登录...参考了这篇教程-->https://www.youtube.com/watch?v=j0wH0m_xYLs
我还没有找到任何有关使用ViewModel进行登录的教程。
找到了这个stackoverflow问题,但是它仍未得到解答--> How to make retrofit API call request method post using LiveData and ViewModel
这是我的调用活动:
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)
}
}
}
})
}
}}
以下是登录响应(LoginResponse):
data class LoginResponse(val status: Int, val data: Data, val message: String, val user_msg:String)
数据类:
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?
)
关于登录的viewmodel需要帮助。
先行致谢。
在添加epicpandaforce的答案时遇到了以下错误:
在 loginviewmodel 中:
在 loginactivity 中:
1-->
2-->
3-->