Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrofit 2 - How to show progress bar On Receiving JSON Response

I am using multiple Fragments in tabbed activity to show json data.
I want to show progress bar whenever the response is received in every fragment.

private void loadJSON() {
  Retrofit retrofit = new Retrofit.Builder()
                                  .baseUrl(BASEURL)
                                  .addConverterFactory(GsonConverterFactory.create())
                                  .build();
  newsAPI = retrofit.create(NewsAPI.class);
  Call<JSONResponse> call = newsAPI.topNews("soure", "api-key");

  call.enqueue(new Callback<JSONResponse>() {
    @Override
    public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response) {
      Log.v("this", "Yes!");
    }

    @Override public void onFailure(Call<JSONResponse> call, Throwable t) {
      Log.v("this", "No Response!");
    }
  });
}
like image 737
Omer Danish Avatar asked Feb 17 '17 15:02

Omer Danish


2 Answers

With something like this, using progressDialog :

private void loadJSON() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        newsAPI = retrofit.create(NewsAPI.class);
        Call < JSONResponse > call =
                newsAPI.topNews("soure", "api-key");

        // Set up progress before call
        final ProgressDialog progressDoalog;
        progressDoalog = new ProgressDialog(MainActivity.this);
        progressDoalog.setMax(100);
        progressDoalog.setMessage("Its loading....");
        progressDoalog.setTitle("ProgressDialog bar example");
        progressDoalog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        // show it
        progressDoalog.show();

        call.enqueue(new Callback < JSONResponse > () {

            @Override
            public void onResponse(Call < JSONResponse > call, Response < JSONResponse > response) {
                // close it after response
                progressDoalog.dismiss();
                Log.v("this", "Yes!");
            }
        }

        @Override public void onFailure(Call < JSONResponse > call, Throwable t) {
            // close it after response
            progressDoalog.dismiss();
            Log.v("this", "No Response!");
        }
    });
like image 55
Cedric Franck Avatar answered Oct 19 '22 23:10

Cedric Franck


Simple Trick progressBar in your layout

  <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="?attr/actionBarSize"
        android:layout_weight="1" />

Initialize the progressbar

 loadProgress = findViewById(R.id.progressBar);

Finally make it gone after json load competed

 loadProgress.setVisibility(View.GONE);

then make it visible in some user interaction, then finally gone when loading of json data is completed. Simple fake trick

like image 41
Suman Astani Avatar answered Oct 19 '22 22:10

Suman Astani