4

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....

this method model.ResponseData.observeis not getting call

i dont know where im going wrong

need help thanks

Loginactivity:--

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)

        }
    }
}}

viewmodel:--

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)
                    }
                }
            }
        })
}
}
IRON MAN
  • 191
  • 3
  • 18
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/223275/discussion-on-question-by-iron-man-user-login-is-not-working-using-retrofit-and). – Samuel Liew Oct 19 '20 at 08:59

2 Answers2

2

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()

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

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

Jeel Vankhede
  • 11,592
  • 2
  • 28
  • 58
0

Try this

 if (res.body()?.status == 200) {
                _aResponseData.postValue(response.body()) 
            }
Quentin vk
  • 313
  • 3
  • 9