Problem with Asynctask that when inserting in database does not pass to the next activity

1

I have a login form for a user that is connected to a MySQL database.

The problem is that when I insert it, it does not happen to the next activity. The problem is in onPostExecute in my opinion, any ideas?

private class Asyncinsert extends AsyncTask<String, String, String>
{

    ProgressDialog pdLoading = new ProgressDialog(formularioAsistente.this);
    HttpURLConnection conn;
    URL url = null;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        pdLoading.setTitle("Verificando");
        pdLoading.setMessage("\tUn Momento...");
        pdLoading.setCancelable(false);
        pdLoading.show();

    }
    @Override
    protected String doInBackground(String... params) {
        try {


            url = new URL("http://bdauditorio.esy.es/asistente/asistenteinsert.php");

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "exception";
        }
        try {

            conn = (HttpURLConnection)url.openConnection();
            conn.setReadTimeout(READ_TIMEOUT);
            conn.setConnectTimeout(CONNECTION_TIMEOUT);
            conn.setRequestMethod("POST");


// setDoInput y setDoOutput representan el manejo de los mensajes de envío y recepción
            conn.setDoInput(true);
            conn.setDoOutput(true);

            // Añado parámetros a la URL
            Uri.Builder builder = new Uri.Builder()
                    .appendQueryParameter("primer_nombre", params[0])
                    .appendQueryParameter("primer_apellido", params[1])
                    .appendQueryParameter("email", params[2])
                    .appendQueryParameter("tipo_asistente", params[3]);
            String query = builder.build().getEncodedQuery();

            // Abro conexion para enviar datos
            OutputStream os = conn.getOutputStream();
            BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(os, "UTF-8"));
            writer.write(query);
            writer.flush();
            writer.close();
            os.close();
            conn.connect();

        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
            return "exception";
        }

        try {

            int response_code = conn.getResponseCode();

// Compruebo si se ha realizado correctamente la conexion

            if (response_code == HttpURLConnection.HTTP_OK) {


// Leo los datos enviados desde el servidor
                InputStream input = conn.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(input));
                StringBuilder result = new StringBuilder();
                String line;

                while ((line = reader.readLine()) != null) {
                    result.append(line);
                }


// Paso los datos al método onPostExecute
                return(result.toString());

            }else{

                return("unsuccessful");
            }

        } catch (IOException e) {
            e.printStackTrace();
            return "exception";
        } finally {
            conn.disconnect();
        }


    }
 @Override
    protected void onPostExecute(String result) {

        // este método se ejecutará en el subproceso de la interfaz de usuario
        pdLoading.dismiss();

        if(result.equalsIgnoreCase("true"))
        {
           // Toast.makeText(getApplicationContext(),"Bienvenido: "+email,Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(formularioAsistente.this,ParticiparEvento.class);
            startActivity(intent);




        }else if (result.equalsIgnoreCase("false")){

            final AlertDialog.Builder alertaDeError = new AlertDialog.Builder(formularioAsistente.this);
            alertaDeError.setTitle("Error");
            alertaDeError.setMessage("Ups, intente nuevamente.");
            alertaDeError.setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                }
            });
            alertaDeError.create();
            alertaDeError.show();

        } else if (result.equalsIgnoreCase("exception") || result.equalsIgnoreCase("unsuccessful")) {


            final AlertDialog.Builder alertaDeError2 = new AlertDialog.Builder(formularioAsistente.this);
            alertaDeError2.setTitle("Error");
            alertaDeError2.setMessage("Ha ocurrido un error inesperado. Intente nuevamente.");
            alertaDeError2.setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                }
            });
            alertaDeError2.create();
            alertaDeError2.show();


        }
    }

}
    
asked by Ashley G. 15.12.2016 в 20:50
source

1 answer

0

Do not perform Intent simply because the variable% co_of% that you receive in result does not contain the value onPostExecute() .

 if (result.equalsIgnoreCase("true")) {

Inside doInBackground () review here:

// Paso los datos al método onPostExecute
   return(result.toString());

The only way that Intent does not run is that "true" does not contain the value result .

You can add 2 validations, convert to lowercase and eliminate spaces, within the variable.

 if (result.equalsIgnoreCase("true").toLowerCase().trim()) {
    
answered by 15.12.2016 в 20:57