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

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


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

Related posts

Leave a Comment