Search code examples
androidandroid-asynctaskresultset

AsyncTask and ResultSet on android


I have a problem when I use the AsyncTask, this code works fine, but when I return a ResultSet, in the mainActivity i haven't the result of the AsyncTask, and my app break. I try using a timer and a Thread.sleep and don't work

     blast_dbConnect task = new blast_dbConnect("asd","123",MainActivity.this);
    task.execute("Conectando",sql);
try {
        if(rs.next()==true && rs!=null){

            String passCheck;
            passCheck = rs.getString("password");
            Log.i("Se conecta",passCheck);
            tv3.setText(passCheck);


        /*catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        */
        /*if(passCheck.equals(password)){
            Log.i("Se conecta","Se conecta");
            return rs;
        }*/

        }
    } catch (SQLException e) {
            // TODO Auto-generated catch block
        e.printStackTrace();
    }

This is my synctask code rs is a ResultSet (I want that my synctask return a resultset. My synctask is this

        public class blast_dbConnect extends AsyncTask<String,String,ResultSet> {
String username;
String password;
Context c;
ProgressDialog pd;
private String ipServidorMySQL = "jdbc:mysql://db4free.net:3306/cuetospalace";
private String contrasenaMySQL="636251630";
private String usuarioMySQL = "sdelcueto";


public blast_dbConnect(String user,String pass,Context context){
     username = user;
     password = pass;
     c = context;
}
@Override
protected void onPreExecute(){
    pd = new ProgressDialog(c);
    pd.setMessage("Conectando");
    pd.setCancelable(false);
    pd.show();

}
@Override
protected ResultSet doInBackground(String...params) {
    boolean login = false;
    String input = params[1];
    //publishProgress(input);
    try{
         try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Connection conn;
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        //conn = DriverManager.getConnection("jdbc:mysql://db4free.net:3306/cuetospalace","sdelcueto","636251630");
        conn =  (Connection)DriverManager.getConnection(ipServidorMySQL, usuarioMySQL, contrasenaMySQL);
        //Log.i("SQL",input);

        Statement stmt = conn.createStatement();
        //Log.i("Se conecta","Se conecta");
        ResultSet rs = stmt.executeQuery(input);
        /*if(rs.next()==true){


            String passCheck = rs.getString("password");

            return passCheck;
            /*if(passCheck.equals(password)){
                Log.i("Se conecta","Se conecta");
                return rs;
            }*/

        //}
        return rs;

    }
    catch(SQLException | InstantiationException | IllegalAccessException | ClassNotFoundException e){
        //Log.i("bla",e.getMessage());
        return null;
    }

}
@Override
protected void onProgressUpdate(String...params){
    //Log.i("onProgressUpdate","onProgressUpdate");
    pd.setMessage(params[0]);
}

protected void onPostExecute(ResultSet rs){

    //Log.i("onPostExecute","onPostExecute");
    MainActivity.rs=rs;
    if(pd != null)
        pd.dismiss();
    //pd.cancel();


}
protected void onCancelled (){
    //Log.i("onCancelled","onCancelled");
    cancel(true);
}

}' I want to he main thread wait for the AsyncTask finish, I try the get function too Can anyone help me?


Solution

  • if you want main thread wait for asyncTask , so why not do your work in main thread?

    if you want that you can run a ProgressDialog with cancelable= false in onPreExecute() and then in onPostExecute() dismis that dialog.

    class MyAsync extends AsyncTask<String, Void, String> {
    
        ProgressDialog progressDialog;
    
        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            progressDialog = new ProgressDialog(Activity.this);
            progressDialog.setMessage("please wait...");
    
            progressDialog.setCancelable(false);
            progressDialog.show();
        }
    
        @Override
        protected String doInBackground(String... params) {
             //background task
        }
    
        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            if(progressDialog != null)
                progressDialog.dismiss();
    
        }
    
    }  
    

    UPDATE:
    in your code:

     public void onCreate(Bundle savedInstanceState){    
        blast_dbConnect task = new      blast_dbConnect("asd","123",MainActivity.this);
       task.execute("Conectando",sql);
     }
    
     public void useRS(ResultSet rs){
        this.rs = rs;
        try {
           if(rs!=null && rs.next()==true){
    
              String passCheck;
              passCheck = rs.getString("password");
              Log.i("Se conecta",passCheck);
              tv3.setText(passCheck);
    
    
             /*catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
             }
            */
           /*if(passCheck.equals(password)){
               Log.i("Se conecta","Se conecta");
               return rs;
           }*/
    
           }
        } catch (SQLException e) {
             // TODO Auto-generated catch block
            e.printStackTrace();
        }
     } 
    

    in onPostExecute():

    onPostExecute(ResultSet rs){
       if(pd!=null)
          pd.dismis();
       useRS(rs);
    }