Retrofit 2 - 如何在接收JSON响应时显示进度条

时间:2017-02-17 15:37:14

标签: android retrofit2

我在选项卡式活动中使用多个片段来显示json数据 我想在每个片段收到响应时显示进度条。



install.packages("rmarkdown", repos="https://cloud.r-project.org")




2 个答案:

答案 0 :(得分:14)

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

答案 1 :(得分:2)

简单的伎俩 布局中的progressBar

  <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" />

初始化进度条

 loadProgress = (ProgressBar) findViewById(R.id.progressBar);

最后在json load竞赛后消失

 loadProgress.setVisibility(View.Gone);

然后在某些用户交互中使其可见,然后在加载json数据完成后最终消失。简单的假伎俩