Skip to content Skip to sidebar Skip to footer

Login Example Using Retrofit, Mvvm, Livedata In Android

I checked this article but observe the response changes in MainActivity. Here is my code for LoginRepo public MutableLiveData checkLogin(LoginRequestModel

Solution 1:

Here is what i have tried using your classes just altering retrofit to background thread to wait 5 seconds and then setting the data (you need to confirm the response being successful as you don't change the data if it's failing and hence if the loginResponseModel is null then it will enter the onChanged Method but it won't do anything as you don't have a condition if it is equals to null) here is what i did

in Main Activity -> onCreate() i just created the viewmodel and observed on the mutableLiveData

myViewModel.onLoginClick(null);
        myViewModel.simpleModelMutableLiveData.observe(this, newObserver<String>() {
            @OverridepublicvoidonChanged(@NullableString s) {
                if(s==null)
                  Log.v("testinggg","test - onChanged --- Null " );
                elseLog.v("testinggg","test - onChanged --- s -> "+s );
            }
        });

Then here is the ViewModel -> in which you will have the MutableLiveData itself named simpleModelMutableLiveData

MutableLiveData<String> simpleModelMutableLiveData;


    publicLiveData<String> getUser() {
        if (simpleModelMutableLiveData == null) {
            simpleModelMutableLiveData = newMutableLiveData<>();

        }

        return simpleModelMutableLiveData;
    }

  // this method will return Object of MutableLiveData<String> and let the simpleModelMutableLiveData be the returned objectprivatevoidcheckLogin(String placeholder) {
        simpleModelMutableLiveData = MyRepo.checkLogin(placeholder);
    }

    publicvoidonLoginClick(View view) {

        checkLogin("test");
    }

and at last the Repo method in which i will return the MutableLiveData and let the simpleModelMutableLiveData to be the return and initiate a background thread using runnable that will wait 5 seconds before it sets the value using a handler (in your case you will need to set the value of the data after enqueue inside the Overridden Methods onResponse and onFailure)

as follows

publicstaticMutableLiveData<String> checkLogin(String test) {
        final MutableLiveData<String> data = newMutableLiveData<>();


        Runnable r = newRunnable() {
            publicvoidrun() {
                runYourBackgroundTaskHere(data);
            }
        };

        newThread(r).start();



        return data;
    }

     privatestaticvoidrunYourBackgroundTaskHere(final MutableLiveData<String> data) {

        try {
            Thread.sleep(5000);
//            Handler handler  = new Handler();newHandler(Looper.getMainLooper()).post(newRunnable() {
                @Overridepublicvoidrun() {
                    // things to do on the main thread/* Here i set the data to sss and then null and when 
                   you check the logcat and type the keyword used for 
                   logging which is "testinggg" 
                   you will find it show sss and then null which means 
                   it has entered the onChanged and showed you the log */

                    data.setValue("sss"); 
                    data.setValue(null);
                }
            });

        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

Post a Comment for "Login Example Using Retrofit, Mvvm, Livedata In Android"