简体   繁体   English

Android AsyncTask onPostExecute调用晚了吗?

[英]Android AsyncTask onPostExecute called late?

My activity creates an instance of my separate AsyncTask class and executes it. 我的活动创建了我单独的AsyncTask类的实例并执行它。 DoInBackground runs fine alongside the main activity in separate threads, but onPostExecute is only called after everything has finished in the UI thread. DoInBackground与主要活动在单独的线程中运行良好,但是仅在UI线程中的所有操作完成后才调用onPostExecute。 To my knowledge, this should not be the case: the AsyncTask should move to onPostExecute as soon as doInBackground has finished and returned a value. 据我所知,事实并非如此:AsyncTask应该在doInBackground完成并返回值后立即移至onPostExecute。 Here's my code, I'd really appreciate it if anyone could tell me what's going on here: 这是我的代码,如果有人可以告诉我这里发生了什么,我将非常感激:

Firstly my AsyncTask: 首先我的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);
    }
}

note - I had to comment out the listener as it relied on onPostExecute working as intended 注意-我不得不注释掉监听器,因为它依赖于onPostExecute正常工作

Now the main activity (relevant subroutine): 现在的主要活动(相关子例程):

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

and finally the logcat: 最后是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

The last system out is from the onPostExecute, after the lengthy process on the UI thread has finished. UI线程上的冗长过程完成后,最后一个系统来自onPostExecute。 Any help would be appreciated! 任何帮助,将不胜感激!

onPostExecute() actually runs in the main UI thread- this is so you can interact with elements that can only be edited from there, such as text on TextViews , clearing away a ProgressBar , etc. The order in which it runs depends on Android itself, although it tends to be at the end of the thread's queue. onPostExecute()实际上在主UI线程中运行-因此,您可以与只能从那里进行编辑的元素进行交互,例如TextViews文本,清除ProgressBar等。它的运行顺序取决于Android本身,尽管它通常位于线程队列的末尾。

Here are a few simple fixes: 以下是一些简单的修复程序:

  • Do what you would like to do in the non-UI thread from within doInBackground() , if possible 如果可能,请在doInBackground()中在非UI线程中执行您想做的事情
  • Start up another AsyncTask from the end of onPostExecute() and pass it any necessary data onPostExecute()的末尾启动另一个AsyncTask并将任何必要的数据传递给它
  • Start up your own new thread+runnable 启动自己的新线程+可运行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM