AsyncTask class in android Java with its methods does not complete the process

0

I have a process that I want to run in the background, it works but at the end I want it to go to the next activity but it does not, it gives me a black screen and when it finishes the task goes to the activity: within the class I have several cycles for which they run one after the other: here my code:

class pruebaAsync extends AsyncTask<Void, Integer, Boolean> {
    @Override
    protected void onPreExecute() {
        //super.onPreExecute();
        Toast.makeText(getApplicationContext(),"Inicia",Toast.LENGTH_SHORT).show();
    }

    @Override
    protected Boolean doInBackground(Void... voids) {
            String url = "http://101.168.101.80/tablets/catalogos.php?imei=" + getIMEINumber();
            RequestQueue cola = Volley.newRequestQueue(getApplicationContext());
            StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {

                            try {
                                JSONObject jsTotal       = new JSONObject(response);
                                JSONArray jaPaises       = jsTotal.getJSONArray("paises");
                                JSONArray jaProcedencia  = jsTotal.getJSONArray("procedencia");
                                JSONArray jaClave        = jsTotal.getJSONArray("clave");
                                JSONArray jaDepartamento = jsTotal.getJSONArray("departamento");
                                JSONArray jaMunicipio    = jsTotal.getJSONArray("municipio");
                                JSONArray jaCanton       = jsTotal.getJSONArray("canton");
                                JSONArray jaCaserio      = jsTotal.getJSONArray("caserio");
                                JSONArray jaTablet       = jsTotal.getJSONArray("tablet");
                                JSONArray jaInstitucion  = jsTotal.getJSONArray("institucion");
                                JSONArray jaTipo         = jsTotal.getJSONArray("tipoEstablecimiento");
                                JSONArray jaEst          = jsTotal.getJSONArray("establecimiento");


                                for (int i = 0; i <jaPaises.length() ; i++) {
                                    JSONObject joPais = jaPaises.getJSONObject(i);
                                    saveCoutry(joPais.getLong("id"), joPais.getString("nombre"),joPais.getInt("activo"));
                                }


                                for (int j = 0; j < jaProcedencia.length() ; j++) {
                                    JSONObject joProcedencia = jaProcedencia.getJSONObject(j);
                                    saveProcedencia(joProcedencia.getLong("id"), joProcedencia.getString("nombre"));
                                }

                                for (int k = 0; k <jaClave.length() ; k++) {
                                    JSONObject joClave = jaClave.getJSONObject(k);
                                    saveClave(joClave.getLong("id"),joClave.getInt("id_departamento"),joClave.getInt("id_municipio"),
                                            joClave.getInt("correlativo"),joClave.getString("clave"),joClave.getLong("id_procedencia"));
                                }
                                for (int l = 0; l < jaDepartamento.length() ; l++) {
                                    JSONObject joDepto = jaDepartamento.getJSONObject(l);
                                    saveDepartamento(joDepto.getLong("id"),joDepto.getString("nombre"),joDepto.getLong("id_pais"));
                                }
                                for (int m = 0; m <jaMunicipio.length() ; m++) {
                                    JSONObject joMpo = jaMunicipio.getJSONObject(m);
                                    int  id_depto_apoyo;
                                    if (!joMpo.isNull("id_depto_apoyo")) {
                                        id_depto_apoyo = joMpo.getInt("id_depto_apoyo");
                                    }else{
                                        id_depto_apoyo =0;
                                    }
                                    saveMunicipio(joMpo.getLong("id"),joMpo.getString("nombre"),joMpo.getLong("id_departamento"), id_depto_apoyo);
                                }
                                for (int n = 0; n <jaCanton.length() ; n++) {
                                    JSONObject joCanton = jaCanton.getJSONObject(n);
                                    saveCanton(joCanton.getLong("id"),joCanton.getString("nombre"),joCanton.getLong("id_municipio"));
                                }
                                for (int o = 0; o <jaCaserio.length() ; o++) {
                                    JSONObject joCaserio = jaCaserio.getJSONObject(o);
                                    int  id_depto_apoyo;
                                    if (!joCaserio.isNull("id_depto_apoyo")) {
                                        id_depto_apoyo = joCaserio.getInt("id_depto_apoyo");
                                    }else{
                                        id_depto_apoyo =0;
                                    }
                                    int  bandera;
                                    if (!joCaserio.isNull("bandera")) {
                                        bandera = joCaserio.getInt("bandera");
                                    }else{
                                        bandera =0;
                                    }
                                    savecaserio(joCaserio.getLong("id"),joCaserio.getString("nombre"),joCaserio.getLong("id_canton"),
                                            id_depto_apoyo,bandera);
                                }
                                for (int q = 0; q <jaInstitucion.length() ; q++) {
                                    JSONObject joIns =  jaInstitucion.getJSONObject(q);
                                    saveInstitucion(joIns.getLong("id"),joIns.getString("nombre"));
                                }
                                for (int r = 0; r <jaTipo.length() ; r++) {
                                    JSONObject joTipo = jaTipo.getJSONObject(r);
                                    saveTipoestablecimiento(joTipo.getLong("id"),joTipo.getString("nombre"),joTipo.getLong("id_institucion"));
                                }
                                saveMinsal();
                                for (int s = 0; s <jaEst.length() ; s++) {
                                    JSONObject joEst = jaEst.getJSONObject(s);
                                    long  id_municipio;
                                    if (!joEst.isNull("id_municipio")) {
                                        id_municipio = joEst.getLong("id_municipio");
                                    }else{
                                        id_municipio =0;
                                    }

                                    saveEstablecimiento(joEst.getLong("id"),joEst.getString("nombre"),joEst.getString("latitud"),
                                            joEst.getString("longitud"),id_municipio,joEst.getInt("id_establecimiento_padre"),
                                            joEst.getInt("id_tipo_establecimiento"));

                                }

                                for (int p = 0; p <jaTablet.length() ; p++) {
                                    JSONObject joTablet = jaTablet.getJSONObject(p);
                                    saveTablet(joTablet.getLong("id"),joTablet.getLong("id_sibasi"),joTablet.getString("codigo"),joTablet.getString("imei"));
                                }


                            } catch (Exception e) {
                                e.printStackTrace();                        }


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

                    //Toast.makeText(getApplicationContext(), String.valueOf(error), Toast.LENGTH_LONG).show();
                }
            });

            cola.add(stringRequest);


        return true;
    }

    @Override
    protected void onPostExecute(Boolean abolean) {
        //super.onPostExecute(aVoid);
        if (abolean){
            Toast.makeText(getApplicationContext(), "exito", Toast.LENGTH_LONG).show();
            Intent i = new Intent(getApplicationContext(),LoginActivity.class);
            startActivity(i);
        }

    }
}
    
asked by Igmer Rodriguez 23.08.2018 в 00:06
source

1 answer

0

Well in case someone else happens, the situation was that Volley works in a thread apart from the user interface so there was no need to create an asynchronous task since volley runs asynchronously: in the end I have executed the whole process in a voi method and it has worked.

    
answered by 23.08.2018 / 19:21
source