Skip to main content

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

Popular posts from this blog

 var val  deference in kotlin  fun main () { var Android_Programing_language : String = "java" println ( "android official programing language: " + Android_Programing_language ) Android_Programing_language = "Kotline" println ( "2019 google announced android official programing language: " + Android_Programing_language ) val Android_MultiPlatform_language : String = "Dart" println ( "multiple device run : " + Android_MultiPlatform_language ) /* Android_MultiPlatform_language="" /* note: var--------------valu changable note: val--------------valu not changable (final) */ */ }
 Firebase Cloud Messaging With  push notification with Image  public class ImageDownload extends AsyncTask < String , Void , Bitmap >{ @Override protected Bitmap doInBackground ( String ... strings ) { InputStream inputStream ; try { URL url = new URL( strings [ 0 ]) ; try { HttpURLConnection connection = ( HttpURLConnection ) url .openConnection() ; connection .connect() ; inputStream = connection .getInputStream() ; return BitmapFactory . decodeStream ( inputStream ) ; } catch ( IOException e ) { e .printStackTrace() ; } } catch ( MalformedURLException e ) { e .printStackTrace() ; } return null; } @Override protected void onPostExecute ( Bitmap bitmap ) { ShowNotification( bitmap ) ; } }
AppBar ViewPager with  Tab layout in android  XML : <? xml version ="1.0" encoding ="utf-8" ?> <RelativeLayout xmlns: android ="http://schemas.android.com/apk/res/android" xmlns: tools ="http://schemas.android.com/tools" android :layout_width ="match_parent" xmlns: app ="http://schemas.android.com/apk/res-auto" android :id ="@+id/mainrelative_layout_id" android :background ="@color/colorPrimary" android :theme ="@style/Appthemnoactionbar" android :layout_height ="match_parent" tools :context ="com.ruhul.scanner.Fragment_Activity.ListFragment" > <com.google.android.material.appbar.AppBarLayout android :id ="@+id/tablayoutid" android :layout_width ="match_parent" android :background ="@color/colorPrimary" android :layout_height ="?actionBarSize" > ...