按钮单击时执行HTTP请求时未显示进度条

时间:2018-02-10 11:11:58

标签: android progress-bar executorservice

我正在按钮点击执行 const body = new FormData(); body.append("picture", { uri: imageUri, name, type: type }); await fetch(`${cloudFunctionUrl}`, { method: "POST", body, headers: { Accept: "application/json", "Content-Type": "multipart/form-data" } }).then(async() => { // do things }).catch(error => { console.log(error); reject(error); }); ,我正在使用ExecutorService实施请求程序。我有一个HTTP request,它应该在处理请求时开始旋转。现在没有发生这种情况,我看不到旋转的圆圈 这是按钮点击代码:

ProgressBar

为什么submitButton.setOnClickListener((View view) -> { setFieldDetails(); if(// Some condition) // Some Action else if(// Some condition) // Some action else if(// Some condition) //Some Action else { runOnUiThread(() -> progressBar.setVisibility(View.VISIBLE)); signUpAction = signUpAction.createSignUpAction(); Future<Integer> signUpResponse = executorService.submit(signUpAction); progressBar.setVisibility(View.INVISIBLE); try { if(signUpResponse.get() == 200){ executorService.shutdown(); Intent intent = new Intent(this, SignInActivity.class); startActivity(intent); } else // Do something irrelevant } catch (InterruptedException | ExecutionException e) { // Manage Exceptions } } }); 没有显示?
如何让它出现? 我可以在AsyncTask的帮助下完成,但由于实施问题,我不想使用它。我想坚持使用ExecutorService 我也试过在没有ProgressBar的情况下运行它,但结果相同 这是我发送请求的类:

runOnUiThread(Runnable r)

1 个答案:

答案 0 :(得分:0)

将progressbar语句放在最后一个条件块之前的顶部。

submitButton.setOnClickListener((View view) -> {
            setFieldDetails();
            runOnUiThread(() -> progressBar.setVisibility(View.VISIBLE));

            if(// Some condition)
                // Some Action
            else if(// Some condition)
                // Some action
            else if(// Some condition)
                //Some Action
            else {

                signUpAction = signUpAction.createSignUpAction();
                Future<Integer> signUpResponse = executorService.submit(signUpAction);
                progressBar.setVisibility(View.INVISIBLE);
                try {
                    if(signUpResponse.get() == 200){
                        executorService.shutdown();
                        Intent intent = new Intent(this, SignInActivity.class);
                        startActivity(intent);
                    } else
                        // Do something irrelevant 
                } catch (InterruptedException | ExecutionException e) {
                    // Manage Exceptions
                }
            }
        });