cleanrun
cleanrun

Reputation: 746

Why does my progress bar not working properly?

I've been building an app which has Log In functionality. I've tested it but every time i tried to Log In, the progress bar disappeared to quickly (like a quarter second or something), and the response i get from the server is like about 2 seconds after the progress bar disappeared. Here are some of my codes.

My LoginTask inner class :

private class LoginTask extends AsyncTask<Account, Void, Account>{
        private String getUsername = username.getText().toString();
        private String getPassword = password.getText().toString();

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            //showDialog();
            progressBar.setVisibility(View.VISIBLE);
        }

        @Override
        protected void onPostExecute(Account account) {
            super.onPostExecute(account);
            //dismissDialog();
            progressBar.setVisibility(View.GONE);
        }

        @Override
        protected Account doInBackground(Account... accounts) {
            getLogin(getUsername, getPassword);
            return null;
        }
    }

Login Retrofit call to server

private void getLogin(String email, String password) {
        Call<LoginAuth> call = userService.getLogin(email, password);
        call.enqueue(new Callback<LoginAuth>() {
            @Override
            public void onResponse(Call<LoginAuth> call, Response<LoginAuth> response) {
                try {
                    if (response.body().getToken_type().equals("xxx")) {
                        Log.i(TAG, "getLogin, Authorized access");
                        Log.i(TAG, "getLogin, access_token: " + response.body().getAccess_token().toString());
                        Log.i(TAG, "getLogin, expires_at" + response.body().getExpires_at().toString());
                    } else {
                        Log.e(TAG, "getLogin, Unauthorized access" + response.body().getToken_type().toString());
                    }
                } catch (Exception e) {
                    Log.e(TAG, "getLogin exception " + e.getMessage());
                }
            }

            @Override
            public void onFailure(Call<LoginAuth> call, Throwable t) {
                Log.e(TAG, "getLogin, onFailure : " + t.getMessage());
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(LoginActivity.this, "Unable to Log In :(", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }

I want it to work like when the response is fetched, that's the time the progress bar disappeared (not instantly). Did i do something wrong with the code?

Upvotes: 0

Views: 180

Answers (1)

karan
karan

Reputation: 8853

As you are using retrofit, there is no necessity to call your api in separate asynctask as retrofit manages it asynchronously. what you should do is show your progressbar before you call api and dismiss it in onResponse and onFailure both. so your code would change to something like below.

private void getLogin(String email, String password) {
        progressBar.setVisibility(View.VISIBLE);
        Call<LoginAuth> call = userService.getLogin(email, password);
        call.enqueue(new Callback<LoginAuth>() {
            @Override
            public void onResponse(Call<LoginAuth> call, Response<LoginAuth> response) {
                progressBar.setVisibility(View.Gone);
                //rest of your code
            }

            @Override
            public void onFailure(Call<LoginAuth> call, Throwable t) {
                Log.e(TAG, "getLogin, onFailure : " + t.getMessage());
                progressBar.setVisibility(View.Gone);
                //rest of your code
            }
        });
    }

Upvotes: 1

Related Questions