Retrofit2下载数据异步并获取进度条

时间:2018-06-08 14:39:21

标签: java android retrofit2

我需要在下载Web服务的内容时显示进度条,因为我需要在下载完成时进行片段转换。我尝试过许多不同的事情,但我仍然坚持起步。我在下载完成之前完成了转换

有人可以给我一个例子或向我展示一个方向,从哪里寻找解决这个问题的方法?

 public void efetuarLogin() {
    loadProgress.setVisibility(View.VISIBLE);
    final iRequestsTramita requestsTramita = TramitaService.getInstance().usarServico();
    Call<RespostaTramita> callCapturarUG = requestsTramita.getResposta(new LoginTramita(userLogin,userPass));
    callCapturarUG.enqueue(new Callback<RespostaTramita>() {
        @Override
        public void onResponse(Call<RespostaTramita> call, Response<RespostaTramita> response) {
            //loadProgress.setVisibility(View.GONE);
            if (response.isSuccessful()) {
                doing something...
                baixarObrasGestor();
                loadProgress.setVisibility(View.GONE);

            } else {
                doing something...
            }

        }

        @Override
        public void onFailure(Call<RespostaTramita> call, Throwable t) {
           ...
        }
    });
}

public void baixarObrasGestor(){
    List<Gestao> listaIdRequisicao = bancoDadosController.gerarListaRequisicaoGeoPB(userLogin);
    iRequestsGeoPB requestsGeoPB = GeoPBService.getInstance().usarServico();
    //make some requests to webservice
    for (Gestao gestao : listaIdRequisicao){
        ...
        Call<List<RespostaGeoPB>> callBaixarObras = requestsGeoPB.baixarObrasGestor(headers, paramRequisicao);
        callBaixarObras.enqueue(new Callback<List<RespostaGeoPB>>() {
            @Override
            public void onResponse(Call<List<RespostaGeoPB>> call, Response<List<RespostaGeoPB>> response) {
                ...doing something
            }

            @Override
            public void onFailure(Call<List<RespostaGeoPB>> call, Throwable t) {
                Log.d("TESTE", "erro: " + t.getMessage());
            }
        });
    }
    selecionaUG(); //do  the fragment transition
}

1 个答案:

答案 0 :(得分:0)

我个人覆盖public abstract class RetryCallback<T> implements Callback<T> { ... }这为您提供了包含您想要提升的所有自定义事件的选项。

onResponse

如果您只想在加载完成时听到这么多skleton就足够了。但请确保您的onFailuretry-catch不会引发异常。当引发一些异常时,我经常看到转换,恕不另行通知。将您的代码打包在public void onResponse(Call<T> call, Response<T> response) { if(!response.isSuccessful()){ // .... loadingComplete(); return; } // ... loadingComplete(); } @Override public void onFailure(Call<T> call, Throwable t) { // ... loadingComplete(); } 中,看看是不是问题。

{{1}}