Android AsyncTask onPostExecute调用迟到了吗?

时间:2015-02-01 19:40:46

标签: java android multithreading android-asynctask

我的活动创建了一个单独的AsyncTask类的实例并执行它。 DoInBackground在主要活动中与单独的线程一起运行良好,但onPostExecute仅在UI线程中的所有内容完成后调用。据我所知,情况并非如此:只要doInBackground完成并返回一个值,AsyncTask就应该转移到onPostExecute。这是我的代码,如果有人能告诉我这里发生了什么,我真的很感激:

首先是我的AsyncTask:

package com.theo.mcginley.comp4prototype;

import android.os.AsyncTask;
import java.sql.*;


public class DBConnector extends AsyncTask <String, Void, ResultSet> {

    private Connection con;
    private Statement st;
    private ResultSet rs;
  //  public CallBackListener DBlistener;



    public ResultSet getMyResults(){
        return rs;
    }

    //public


    @Override
    protected ResultSet doInBackground(String... params) {

String Query = params[0];
        System.out.println("Starting DBConnector");
        try{
            Class.forName("com.mysql.jdbc.Driver");
        }catch(Exception ex){
            System.out.println("Error: "+ex);
        }


        try{
            con = DriverManager.getConnection("jdbc:mysql://musictimetables.cdo3zf9dx3fx.us-west-2.rds.amazonaws.com:3306/music?user=Admin&password=password");
            st = con.createStatement();
        }catch(Exception ex) {
            System.out.println("Error: " + ex);
        }

        System.out.println("Connected");


        try{
            rs = st.executeQuery(Query);
            System.out.println("Records from Database");
        }catch(Exception ex){
            System.out.println(ex);
        }

        System.out.println("Finished DBConnector");
        if (rs==null){
            System.out.println("ITS NULL");
        } else{
            System.out.println("ITS NOT NULL");
        }

        return rs;
    }

    @Override
    protected void onPostExecute(ResultSet myrs) {
      //  super.onPostExecute(myrs);
        System.out.println("THIS IS WHERE IT ALL GOES WRONG");
      //  DBlistener.processFinish(myrs);
    }
}

注意 - 我必须注释掉侦听器,因为它依赖onPostExecute按预期工作

现在主要活动(相关子程序):

private void BuildTable(int rows, int cols, String GivenTutorID) {
        DBConnector connect = new DBConnector();
        String StudentID = "";
        String TutorID = "";
        String VenueLocation = "";
        String LessonDate = "";
        String LessonStartTime = "";
        String LessonEndTime = "";
        List<String> AllTheDates = new ArrayList<String>();
        Integer count = 0;

        connect.execute("SELECT * FROM Lessons WHERE Lessons.TutorID = '" + GivenTutorID + "';");
        try {
            Thread.sleep(4000);//just used so that the resultset doesn't get no results - waits for connect.execute to finish.
        } catch (Exception e) {
        }

        try {
            Thread.sleep(4000);//just used so that the resultset doesn't get no results - waits for connect.execute to finish.
        } catch (Exception e) {
        }
        try {
            Thread.sleep(4000);//just used so that the resultset doesn't get no results - waits for connect.execute to finish.
        } catch (Exception e) {
        }




        System.out.println("Attempting to get ResultSet");

        try {
            rs = connect.getMyResults();
            System.out.println("so far so good");
        } catch(Exception e){
            System.out.println("caught exception1: " + e);
        }

        System.out.println("through to the other side");
        System.out.println("Finished DBConnector");

        if (rs==null){
            System.out.println("ITS STILL NULL");
        } else{
            System.out.println("ITS STILL NOT NULL");
        }

            try {
                while (rs.next()) {
                    StudentID = rs.getString("StudentID");
                    TutorID = rs.getString("TutorID");
                    VenueLocation = rs.getString("VenueLocation");
                    LessonDate = rs.getString("LessonDate");
                    LessonStartTime = rs.getString("LessonStartTime");
                    LessonEndTime = rs.getString("LessonEndTime");
                    AllTheDates.add(count,LessonDate);
                    count++;

                }
            }catch(Exception ex) {
                System.out.println("caught exception2: " + ex + "  xd xDDD   " +count);
            }
        System.out.println("ejgwiw");
        BuildRow(AllTheDates.toArray(new String[AllTheDates.size()]));

}


    private void BuildRow(String...params){
        TableRow row = new TableRow(this);
        row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
    try {
        for (Integer i = 0; i < params.length; i++) {
            TextView tv = new TextView(this);
            tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));
            tv.setBackgroundResource(R.drawable.cell_shape);
            tv.setPadding(5, 5, 5, 5);
            tv.setText(params[i]);
            System.out.println(params[i] + " run " + i);
            row.addView(tv);
        }
        try {
            tblTutorTimetable.addView(row);
        } catch (Exception ex) {
            System.out.println(ex);
        }
    } catch (Exception ex){
        System.out.println(ex);
    }
    }

最后是logcat:

02-01 19:26:45.092  10317-10494/com.theo.mcginley.comp4prototype I/System.out﹕ Starting DBConnector
02-01 19:26:48.223  10317-10494/com.theo.mcginley.comp4prototype I/System.out﹕ Connected
02-01 19:26:48.423  10317-10494/com.theo.mcginley.comp4prototype I/System.out﹕ Records from Database
02-01 19:26:48.423  10317-10494/com.theo.mcginley.comp4prototype I/System.out﹕ Finished DBConnector
02-01 19:26:48.423  10317-10494/com.theo.mcginley.comp4prototype I/System.out﹕ ITS NOT NULL
02-01 19:26:57.462  10317-10317/com.theo.mcginley.comp4prototype I/System.out﹕ Attempting to get ResultSet
02-01 19:26:57.462  10317-10317/com.theo.mcginley.comp4prototype I/System.out﹕ so far so good
02-01 19:26:57.462  10317-10317/com.theo.mcginley.comp4prototype I/System.out﹕ through to the other side
02-01 19:26:57.462  10317-10317/com.theo.mcginley.comp4prototype I/System.out﹕ Finished DBConnector
02-01 19:26:57.462  10317-10317/com.theo.mcginley.comp4prototype I/System.out﹕ ITS STILL NOT NULL
02-01 19:26:57.482  10317-10317/com.theo.mcginley.comp4prototype I/System.out﹕ ejgwiw
02-01 19:26:57.492  10317-10317/com.theo.mcginley.comp4prototype I/System.out﹕ 2015-01-05 run 0
02-01 19:26:57.492  10317-10317/com.theo.mcginley.comp4prototype I/System.out﹕ 2015-01-05 run 1
02-01 19:26:57.492  10317-10317/com.theo.mcginley.comp4prototype I/Choreographer﹕ Skipped 757 frames!  The application may be doing too much work on its main thread.
02-01 19:26:57.542  10317-10317/com.theo.mcginley.comp4prototype I/System.out﹕ THIS IS WHERE IT ALL GOES WRONG

在UI线程上的冗长进程完成后,最后一次系统输出来自onPostExecute。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

onPostExecute()实际上在主UI线程中运行 - 这样您就可以与只能在那里编辑的元素进行交互,例如TextViews上的文字,清除ProgressBar它运行的顺序取决于Android本身,尽管它往往位于线程队列的末尾。

以下是一些简单的修复:

  • 如果可能的话,在doInBackground()内的非UI线程中执行您想要执行的操作
  • onPostExecute()的末尾启动另一个AsyncTask并将其传递给任何必要的数据
  • 启动您自己的新主题+ runnable