Restapi Data get the server in the Android app MVVM model layer(repository) with a background thread
Executors Services background Thread
public static Context context;
public static Upcomming_movie_repository upcomming_movie_repositoryinstance;
public static List<Result> mresult;
private static Upcomingmovie upcomingmovie;
private static MutableLiveData <List<Result>> mutableLiveData;
public static Upcomming_movie_repository getInstance(Context mcontext)
{
if (upcomming_movie_repositoryinstance==null)
{
upcomming_movie_repositoryinstance = new Upcomming_movie_repository();
context = mcontext;
}
return upcomming_movie_repositoryinstance;
}
public MutableLiveData <List<Result>> getupcommingmovie()
{
if (mutableLiveData == null )
{
mutableLiveData = new MutableLiveData<>();
}
int courses = Runtime.getRuntime().availableProcessors();
Log.d("available_processors",String.valueOf(courses));
ExecutorService executorService = Executors.newScheduledThreadPool(courses+1);
Handler handler = new Handler(Looper.myLooper());
executorService.execute(new Runnable() {
@Override
public void run() {
Log.d("slider_mov_thread",Thread.currentThread().getName());
ApiServices apiServices = Retrofitinstance.getRetrofit().create(ApiServices.class);
Call<Upcomingmovie> res = apiServices.getallUpcomming_movie();
res.enqueue(new Callback<Upcomingmovie>() {
@Override
public void onResponse(Call<Upcomingmovie> call, Response<Upcomingmovie> response) {
if (response.isSuccessful())
{
mresult = response.body().getResults();
mutableLiveData.postValue(mresult);
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(context, "data fetch successfully..", Toast.LENGTH_LONG).show();
}
});
// Log.d("mutablelivedata_size",String.valueOf(mutableLiveData.getValue().size()));
}
}
@Override
public void onFailure(Call<Upcomingmovie> call, Throwable t) {
// Toast.makeText(context, "error: "+t.getMessage().toString(), Toast.LENGTH_SHORT).show();
}
});
}
});
return mutableLiveData;
}
Thread and Runnable Background thread
public static Context context;
public static Upcomming_movie_repository upcomming_movie_repositoryinstance;
public static List<Result> mresult;
private static Upcomingmovie upcomingmovie;
private static MutableLiveData <List<Result>> mutableLiveData;
public static Upcomming_movie_repository getInstance(Context mcontext)
{
if (upcomming_movie_repositoryinstance==null)
{
upcomming_movie_repositoryinstance = new Upcomming_movie_repository();
context = mcontext;
}
return upcomming_movie_repositoryinstance;
}
public MutableLiveData <List<Result>> getupcommingmovie()
{
if (mutableLiveData == null )
{
mutableLiveData = new MutableLiveData<>();
}if (mutableLiveData == null)
{
mutableLiveData = new MutableLiveData<>();
}
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
Log.d("thread_name","repository class: "+Thread.currentThread().getName());
ApiServices apiServices = Retrofitinstance.getRetrofit().create(ApiServices.class);
Call<Upcomingmovie> res = apiServices.getallUpcomming_movie();
res.enqueue(new Callback<Upcomingmovie>() {
@Override
public void onResponse(Call<Upcomingmovie> call, Response<Upcomingmovie> response) {
if (response.isSuccessful())
{
upcomingmovie = response.body();
mresult = response.body().getResults();
mutableLiveData.postValue(mresult);
// Log.d("mutablelivedata_size",String.valueOf(mutableLiveData.getValue().size()));
}
}
@Override
public void onFailure(Call<Upcomingmovie> call, Throwable t) {
// Toast.makeText(context, "error: "+t.getMessage().toString(), Toast.LENGTH_SHORT).show();
}
});
}
});
thread.start();
Log.d("thread_last_thread_name","repository class: "+Thread.currentThread().getName());
return mutableLiveData;
}
Asynctask Background Thread :
A deprecated API is one that you are no longer recommended to use, due to changes in the API. While deprecated classes, methods, and fields are still implemented, they may be removed in future implementations, so you should not use them in new code, and if possible rewrite old code not to use them.
public static Context context;
public static Upcomming_movie_repository upcomming_movie_repositoryinstance;
public static List<Result> mresult;
private static Upcomingmovie upcomingmovie;
private static MutableLiveData <List<Result>> mutableLiveData;
public static Upcomming_movie_repository getInstance(Context mcontext)
{
if (upcomming_movie_repositoryinstance==null)
{
upcomming_movie_repositoryinstance = new Upcomming_movie_repository();
context = mcontext;
}
return upcomming_movie_repositoryinstance;
}
public MutableLiveData <List<Result>> getupcommingmovie()
{
if (mutableLiveData == null )
{
mutableLiveData = new MutableLiveData<>();
}Fetchmovie_backgorundtask fetchmovie_backgorundtask = new Fetchmovie_backgorundtask();
fetchmovie_backgorundtask.execute();
return fetchmovie_backgorundtask.doInBackground();
}public static class Fetchmovie_backgorundtask extends AsyncTask<Void,Void,MutableLiveData<List<Result>>>
{
private MutableLiveData <List<Result>> mutableLiveData;
public Fetchmovie_backgorundtask() {
if (mutableLiveData == null )
{
mutableLiveData = new MutableLiveData<>();
}
}
@Override
protected MutableLiveData<List<Result>> doInBackground(Void... voids) {
Log.d("fetchmovie_thread",Thread.currentThread().getName());
ApiServices apiServices = Retrofitinstance.getRetrofit().create(ApiServices.class);
Call<Upcomingmovie> res = apiServices.getallUpcomming_movie();
res.enqueue(new Callback<Upcomingmovie>() {
@Override
public void onResponse(Call<Upcomingmovie> call, Response<Upcomingmovie> response) {
if (response.isSuccessful())
{
upcomingmovie = response.body();
mresult = response.body().getResults();
mutableLiveData.postValue(mresult);
// Log.d("mutablelivedata_size",String.valueOf(mutableLiveData.getValue().size()));
}
}
@Override
public void onFailure(Call<Upcomingmovie> call, Throwable t) {
// Toast.makeText(context, "error: "+t.getMessage().toString(), Toast.LENGTH_SHORT).show();
}
});
return mutableLiveData;
}
}
Comments
Post a Comment