0

I have an asynchronous task in Android Studio, to send and receive data from a server as follows.

 private class myTask extends AsyncTask<Void, Void, Integer> {
        Exception excepccion;

        protected void onPreExecute() {
           super.onPreExecute();
Mensaje.mensajeConectandoseSicoy(actMetodoPago,getString(R.string.msg_conexion_sicoy));
        }
        
        @Override
        protected Integer doInBackground(Void... params) {
        try {
                return new Servidor().guardarObtnerInfoServidorSicoy(ACCION_PAGAR_PEDIDO);
            } catch (Exception e2) {
                excepccion = e2;
                return -1;
            }
        }
        
          @Override
        protected void onPostExecute(Integer respuesta) {
         Mensaje.detenerMensajeConectandoseSicoy();
...
        }

Where:

    public static void mensajeConectandoseSicoy(Context context, String mensaje){
        pd = new ProgressDialog(context);
        pd.setMessage(mensaje);
        pd.setCancelable(false);

        Handler pdCanceller = new Handler();
        pd.show();


    }
    public static void detenerMensajeConectandoseSicoy(){
        if (pd.isShowing()){
            pd.dismiss();
        }
    }

This process works very well until a user reported problems and discovered that their internet is of very poor quality.

When the task is running onPostExecute, that is, when the server has already responded, it is again executed onPreExecute, and this triggers a series of errors.

I solved it by putting a Boolean type flag variable, to avoid the problem.

My question is:

Is there a way to do that control in a better way?

Fabián Romo
  • 319
  • 2
  • 14
  • There must be some culprit in your code . `AsyncTask` works as just a thread it will not restart again never . You need to add minimal code here. – ADM Jun 28 '18 at 16:06
  • How and where you are calling your AsyncTask matters. Share your code. – Pankaj Kumar Jun 28 '18 at 16:06
  • new myTask().execute();//This is only done once – Fabián Romo Jun 28 '18 at 16:09
  • I added a little more code – Fabián Romo Jun 28 '18 at 16:19
  • I think the method from which you are executing the AsunyTask is executing twice. Please share more code. – Raj Jun 28 '18 at 16:25
  • It is a button, you press it to execute the task, then the ProgressDialog blocks the whole screen so that the user can not execute the button again. – Fabián Romo Jun 28 '18 at 16:35
  • Share that buttons code also. – Raj Jun 28 '18 at 17:22
  • private AdapterView.OnClickListener btnContinuarActDetallesproductoServicioOnClickListener = new AdapterView.OnClickListener() { @Override public void onClick(View v) { new myTask().execute(); } – Fabián Romo Jun 28 '18 at 18:57
  • I have checked only with a user whose internet is constantly interrupted, it is as if the network cable were disconnected and connected. For example, I ping www.google.com and the latency is approximately 650 ms to 750 ms – Fabián Romo Jun 28 '18 at 20:29
  • https://stackoverflow.com/questions/6373826/execute-asynctask-several-times – Fabián Romo Jun 29 '18 at 15:49

0 Answers0