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

浪尽此生 提交于 2019-12-07 05:05:56

问题


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!");
    }
  });
}

回答1:


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!");
        }
    });



回答2:


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



来源:https://stackoverflow.com/questions/42301818/retrofit-2-how-to-show-progress-bar-on-receiving-json-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!