用户登录无法使用改造和视图模型 [英] user login is not working using retrofit and viewmodel

查看:28
本文介绍了用户登录无法使用改造和视图模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的登录不起作用..我正在使用改造和视图模型......实际上,scenerio是点击登录显示转换(登录页面到登录页面)但它没有移动到登录页面到主页....

my login is not working ..im using retrofit and viewmodel ...actually scenerio is onclick of login shows the transition(loginpage to loginpage) but it is not moving to loginpage to homepage....

这个方法model.ResponseData.observe没有得到调用

我不知道我哪里错了

需要帮助谢谢

登录活动:--

class LoginActivity : AppCompatActivity() {
lateinit var model: LoginViewModel

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)
    }
    model = ViewModelProvider(this)[LoginViewModel::class.java]

    model.ResponseData.observe(this, object : Observer<LoginResponse?> {
        override fun onChanged(t: LoginResponse?) {
            val intent = Intent(applicationContext, HomeActivity::class.java)
            intent.flags =
                Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
            startActivity(intent)
            finish()
        }

    })
    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
        } else if (password.isEmpty()) {
            loginpassword.error = "Password required"
            loginpassword.requestFocus()
            return@setOnClickListener
        }
        else {
       model.loadAccountData(email,password)

        }
    }
}}

视图模型:--

class LoginViewModel(context: Application,private val savedStateHandle: SavedStateHandle) : AndroidViewModel(context) {
private var _aResponseData = MutableLiveData<LoginResponse?>()

val user: MutableLiveData<String> = savedStateHandle.getLiveData("user", "")
val password: MutableLiveData<String> = savedStateHandle.getLiveData("password", "")
val ResponseData: MutableLiveData<LoginResponse?>



get() {

    if (_aResponseData == null) {
        _aResponseData = MutableLiveData<LoginResponse?>()
        loadAccountData(user.toString(), password.toString())
    }
    return _aResponseData
}


fun loadAccountData(email:String, password:String) {


    RetrofitClient.instance.userLogin(email, password)
        .enqueue(object : Callback<LoginResponse> {
            override fun onFailure(call: Call<LoginResponse>, t: Throwable) {
                Log.d("res", "" + t)
                _aResponseData.value = null
            }

            override fun onResponse(
                call: Call<LoginResponse>,
                response: Response<LoginResponse>
            ) {
                var res = response

                if (res.body()?.status == 200) {
                    _aResponseData.value = response.body()
                } else {
                    try {
                        val jObjError =
                            JSONObject(response.errorBody()!!.string())
                        Toast.makeText(
                            getApplication(),
                            jObjError.getString("user_msg"),
                            Toast.LENGTH_LONG
                        ).show()
                    } catch (e: Exception) {
                        Log.e("errorrr", e.message)
                    }
                }
            }
        })
}
}

推荐答案

经过讨论和调试,看起来问题不在观察者身上,而实际上是在通过改造解析 API 调用的响应期间.

After discussion and debugging, looks like issue was not on observer but actually during parsing of response from API call via retrofit.

_aResponseData.value = response.body()

此处 response.body() 无法将响应解析为 LoginResponse 类对象,这最终导致更多导航问题.

Here response.body() was not able to parse response as LoginResponse class object which was eventually causing issue further more on navigation.

解决解析问题有助于 O.P. 调试主要关注点.

Fixing issue on parsing helps O.P. through debugging on main concern.

这篇关于用户登录无法使用改造和视图模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆