The package With.Examples.Belkhan.androidviewmodel;
Import Android.Arch.Life Cycle.live data;
Import Android.Arch.Life Cycle.MutableLiveData;
Import Android.Arch.Life Cycle.Visual model;
Import Android.Widget.Toast;
Import Java.Useful.The list;
Import Retrofit 2.Call out;
Import Retrofit 2.call back;
Import Retrofit 2.Feedback;
Import Retrofit 2.move back;
Import Retrofit 2.Converter.the resurrection.GsonConverterFactory;
public class HeroViewModel Is spread Visual model {{
// This is the data we will get asynchronously
Private MutableLiveData<The list> hero;
// We will ask this method to get data
public live data<The list> getHeroes()) {{
// if list is null
If ()hero == Invalid) {{
hero = New MutableLiveData<The list>());
// We will load it asynchronously from the server in this method
Loadhiro());
}
// Finally we will return the list
The return hero;
}
// This method is using Retrofit to get JSON data from URL
Private Zero Loadhiro()) {{
move back move back = New move back.Builder())
.Basic()Your.BASE_URL)
.addConverterFactory()GsonConverterFactory.To create()))
.Construction());
Your Your = move back.To create()Your.class);
Call out<The list> Call = Your.getHeroes());
Call.Begrudge()New call back<The list>()) {{
@ Override
public Zero Treason()Call out<The list> Call, Feedback<The list> Feedback) {{
// Finally we are setting the list to our MutableLiveData
hero.To value()Feedback.Body()));
}
@ Override
public Zero Failure()Call out<The list> Call, Throwable T) {{
}
});
}
}
Source link