VOLLEY Android Asynchronic

0

I have the following method that brings me a balance

    String rq = RequestManager.accountBalance(numeroTelefonico);
        GenericController gC = new GenericController(true);
        gC.accBalance(this, rq, new Response.Listener<AccountBalance>() {
            @Override
            public void onResponse(AccountBalance response) {

                if(response.getMessageRS().getErrorCode().compareTo("0")==0){
                  String saldo = "";

                    if(response.getMessageRS().getBalance().compareTo("")==0){
                      saldo =  response.getMessageRS().getAvailableBalance();
                    }
                    else{
                        saldo = response.getMessageRS().getBalance();
                    }}
                else {
                    progressDialog.dismiss();
                }
            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                progressDialog.dismiss();
            }
        });

This I have in the ONCREATE of my class, the problem is that as it is asynchronous it does not get loaded in time and my page continues all its process and at the first time I do not have the visible value, there is some elegant form of make the app wait ah to finish this asynchronous method to continue with the rest of the oncreate?

    
asked by Bruno Sosa Fast Tag 23.01.2018 в 15:34
source

1 answer

1

You have almost implemented it. That is usually done with a ProgressDialog , that's why there is in each part of the peiticón this: progressDialog.dismiss(); . But:

  • in onResponse should not go within a else , but as soon as there is an answer you must invoke dismiss() , so that the dialog is removed, since there is already a response.
  • In onError you have it right.
  • You must show it with progressDialog.show(); once you have added the request to the queue. In that part of the code you must put the names of your real variables for the Volley queue, for the request you have created.
  • I guess you have the ProgressDialog declared higher in your code, more or less like this:

            final ProgressDialog progressDialog = new ProgressDialog(TuActivity.this);
            progressDialog.setMessage("Por favor, espere...");
    
      String rq = RequestManager.accountBalance(numeroTelefonico);
            GenericController gC = new GenericController(true);
            gC.accBalance(this, rq, new Response.Listener<AccountBalance>() {
                @Override
                public void onResponse(AccountBalance response) {
                    progressDialog.dismiss(); //Debes quitarlo en cuanto haya respuesta
                    if(response.getMessageRS().getErrorCode().compareTo("0")==0){
                                String saldo = "";
    
                                if(response.getMessageRS().getBalance().compareTo("")==0){
                                        saldo =  response.getMessageRS().getAvailableBalance();
                                }else{
                                        saldo = response.getMessageRS().getBalance();
                                }
                    }else {
                        /*
                         *Este else puedes usarlo para otra cosa, 
                         *dependiendo de la lógica de tu programa
                         *pero no para el dismiss
                        */ 
                    }
                }
    
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    progressDialog.dismiss();
                }
            });
    
            //En tu código tampoco se ve dónde añades la petición a la cola
            //Ahí debes mostrar el ProgressDialog
    
           requestQueue.add(jsArrayRequest); //Aquí pon las variables requestQueue y jsArrayRequest como las hayas llamado en tu programa
           progressDialog.show();
    
    }//LLave de cierre el onCreate
    

    P.D.: The logic you apply to the onResponse is a bit strange. I did not dare to touch that block. Almost certainly the last else exceeds. I also do not see the way you evaluate the response you receive ... I think that part could be improved, but it depends on what kind of response you expect in the request.

        
    answered by 23.01.2018 / 15:49
    source