安卓上的AsyncTask和ResultSet


我在使用 AsyncTask 时

遇到问题,这段代码工作正常,但是当我返回 ResultSet 时,在 mainActivity 中我没有 AsyncTask 的结果,我的应用程序中断了。我尝试使用计时器和Thread.sleep,但不起作用

     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();
    }

这是我的同步任务代码 rs 是一个结果集(我希望我的同步任务返回一个结果集。我的同步任务是这个

        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);
}

}'我想让他的主线程等待 AsyncTask 完成,我也尝试 get 函数谁能帮我?

如果你想让主线程等待asyncTask,那么为什么不在主线程中做你的工作呢?

如果需要,可以在onPreExecute()中运行带有cancelable= falseProgressDialog,然后在onPostExecute()中消除该对话框。

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();
    }
}  

更新:
在您的代码中:

 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);
}

相关内容

  • 没有找到相关文章

最新更新