doInBackground not executing



我正在开发一个应用程序,该应用程序将启动一个AsyncTask来读取磁卡数据。主线程休眠,并且每隔一秒检查是否有一个布尔标志waitForStripe被设置为false。如果是这样,它将继续下去。我可以看到AsyncTask doInBackground退出,但它从来没有达到onPostExecute。最初我在AsyncTask的onPostExecute中设置了waitForSwipe变量。我通过在doInBackground结束时设置布尔值来解决这个问题,但我对post执行方法没有调用感到困惑。

我包含了一些onPostExecute被正确声明的日志。在这个运行中,onPostExecute设置了caMainWaitForStripe,但似乎仍然没有执行。

10:04:45.940: D/Ca.PocessTrans(5360): CaiSerial  
10:04:45.960: D/Ca.ProcessTrans(5360): wait for swipe, sleeping...:1  
10:04:45.960: D/CaiReader(5360): requesting startSwipeCard in background.  
10:04:45.960: D/CaiReader(5360): doInBackground main thread? false  
10:04:46.000: I/System.out(5360): top of read loop 1, started: false  
10:04:46.520: I/System.out(5360): wait for swipe  
10:04:46.520: I/System.out(5360): wait for start 1  
10:04:46.520: I/System.out(5360): top of read loop 2, started: false  
10:04:46.960: D/Ca.ProcessTrans(5360): wait for swipe, sleeping...:2  
10:04:47.030: I/System.out(5360): wait for swipe  
10:04:47.030: I/System.out(5360): wait for start 2  
10:04:47.030: I/System.out(5360): top of read loop 3, started: false  
10:04:47.540: I/System.out(5360): wait for swipe  
10:04:47.540: I/System.out(5360): wait for start 3  
... here it was just waiting for me to swipe the card  
01-08 10:04:50.090: I/System.out(5360): top of read loop 9, started: false  
01-08 10:04:50.600: I/System.out(5360): wait for swipe  
01-08 10:04:50.600: I/System.out(5360): wait for start 9  
01-08 10:04:50.600: I/System.out(5360): top of read loop 10, started: false  
01-08 10:04:50.960: D/Ca.ProcessTrans(5360): wait for swipe, sleeping...:6  
01-08 10:04:51.110: I/System.out(5360): wait for swipe  
01-08 10:04:51.110: I/System.out(5360): wait for start 10  
01-08 10:04:51.110: I/System.out(5360): top of read loop 11, started: false  
01-08 10:04:51.120: I/System.out(5360): starting data  
01-08 10:04:51.130: I/System.out(5360): read 121 - bytes:121: val: 02  
... echoing the bytes read from card  
01-08 10:04:51.490: I/System.out(5360): read 121 - bytes:121: val: 6F  
01-08 10:04:51.500: I/System.out(5360): read 121 - bytes:121: val: 03  
01-08 10:04:51.510: I/System.out(5360): wait for swipe  
01-08 10:04:51.510: I/System.out(5360): top of read loop 12, started: true  
01-08 10:04:51.520: I/System.out(5360): end data  
01-08 10:04:51.520: I/System.out(5360): Exiting card reader total read 121  
01-08 10:04:51.550: I/System.out(5360): lrc ok  
01-08 10:04:51.550: I/System.out(5360): computed checksum: 6f  
01-08 10:04:51.550: I/System.out(5360): checksum ok  
01-08 10:04:51.680: D/CaiReader(5360): RS232 setting Enc Data  
01-08 10:04:51.680: D/EpayData(5360):  setEncryptedData : good swipe  
.. echoing encrypted data  
01-08 10:04:51.680: D/CaiReader(5360): leaving 'wait for stripe'  
01-08 10:04:51.960: D/Ca.ProcessTrans(5360): wait for swipe, sleeping...:7  
01-08 10:04:52.960: D/Ca.ProcessTrans(5360): wait for swipe, sleeping...:8  
01-08 10:04:53.970: D/Ca.ProcessTrans(5360): wait for swipe, sleeping...:9  
01-08 10:04:54.970: D/Ca.ProcessTrans(5360): wait for swipe, sleeping...:10  
01-08 10:04:55.970: D/Ca.ProcessTrans(5360): wait for swipe, sleeping...:11  
01-08 10:04:56.970: D/Ca.ProcessTrans(5360): wait for swipe, sleeping...:12  
01-08 10:04:57.970: D/Ca.ProcessTrans(5360): wait for swipe, sleeping...:13  
01-08 10:04:58.970: D/Ca.ProcessTrans(5360): wait for swipe, sleeping...:14  
01-08 10:04:59.970: D/Ca.ProcessTrans(5360): wait for swipe, sleeping...:15  
01-08 10:05:00.970: D/Ca.ProcessTrans(5360): wait for swipe, sleeping...:16  
01-08 10:05:01.970: D/Ca.ProcessTrans(5360): wait for swipe, sleeping...:17  
01-08 10:05:02.970: D/Ca.ProcessTrans(5360): wait for swipe, sleeping...:18  
01-08 10:05:03.970: D/Ca.ProcessTrans(5360): wait for swipe, sleeping...:19  
01-08 10:05:04.970: D/Ca.ProcessTrans(5360): wait for swipe, sleeping...:20  
01-08 10:05:05.970: D/Ca.PocessTrans(5360): break timeout 'wait for swipe'  
01-08 10:05:06.120: D/CaiReader(5360): Done, result: Data set  

我完全困惑了,它看起来像onPostExecute被触发为CaiReader (AsyncTask)超出范围,而不是离开后等待滑动。

下面是asyncTask:

class CaiReader extends AsyncTask<EpayData, Long, String> {
    private final String S_TAG = "CaiReader";
    protected void onProgressUpdate(Integer... progress) {
        Log.d("Progress", "Progress");
}
@Override
protected void onPostExecute(String result) {
    Log.d(S_TAG, "Done, result: " + result);
    // caMainWaitSwipe = false;
}
// protected void onPostExecute(Long result) {
//    Log.d(S_TAG, "Done, result: " + result);
//}
/**
 * this is controlling task. It launches  card reader.
*/
@Override
protected String doInBackground(EpayData... params) {
    Log.d(S_TAG, "requesting startSwipeCard in background.");
    showThread(S_TAG, "doInBackground");
    EpayData epayData = params[0];
    byte[] scan = caiSerialReader.readCard();
    if (scan == null) {
        return "failure";
    }
    Map<String, String> rs232Map = CaiRs232Read.parseScan(scan);
    String KSN = rs232Map.get(CaiSerialReader.RS232_KSN);
    String encBlock = rs232Map.get(CaiSerialReader.RS232_ENC_BLOCK);
    Log.d(S_TAG, "RS232 setting Enc Data");
    epayData.setEncryptedData(encBlock, KSN);
    Log.d("CAPTURE", "KSN: " + KSN);
    Log.d("CAPTURE", "encBlock: " + encBlock);
    caMainWaitSwipe = false;
    Log.d(S_TAG, "leaving 'wait for stripe' ");
    String success = "set";
    return success;
}

}

调用程序:

if (readerType == ReaderType.RS232) {
                Log.d("Ca.PocessTrans", "CaiSerial");
                caMainWaitSwipe = true;
                int i = 0;
                CaiReader caiReader = (CaiReader) new CaiReader().execute(epayData);
                while (caMainWaitSwipe) {
                    i++;
                    if (i > 20) {
                        Log.d("Ca.PocessTrans", "break timeout 'wait for swipe'");
                    return DsiParser.timedOutError();
                    }
                    try {
                        Log.d("Ca.ProcessTrans", "wait for swipe,     sleeping...:" + i);
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        Log.d("Ca.ProcessTrans", "process tran     interrupted exception");
                        e.printStackTrace();
                    }
                }
                Log.d("Ca.PocessTrans", "exit rs232 wait for stripe");
            }
            if (!epayData.isReadGood()) {
                return DsiParser.swipeError();
            }
        }
        Log.d("Ca.PocessTrans", "data read");
        authorizeTransaction(epayData);

我认为你的onPostExecute方法签名有问题。参数的类型应该是string,而不是long。我建议将@Override注释放在每个被重写方法的顶部,这样编译器就可以告诉您哪里出错了。另一件事是你不需要一个循环来检查状态,你可以在一个方法中做所有这些,然后在onPostExecute上调用它。

如果你的主线程处于睡眠状态,那么它将不会调用Async,并且你的onPostExecute()将无法到达。让你的doInBackground()每秒钟或任何你想要的时间睡觉,直到达到条件。然后,您可以将此值返回到您的主活动,调用函数或任何您想在UI上做的事情。主线程不应该休眠。或者,您可以在UI

上使用计时器。

目前您没有覆盖AsyncTask onPostExecute(),因为您从doInBackground()返回String,但接收结果为Long,因此更改您的代码为:

class CaiReader extends AsyncTask<EpayData, Long, String> {
    private final String S_TAG = "CaiReader";
    protected void onProgressUpdate(Integer... progress) {
        Log.d("Progress", "Progress");
     }
@Override
protected void onPostExecute(String result) {  //<< chnage to String
    Log.d(S_TAG, "Done, result: " + result);
}
/**
 * this is controlling task. It launches  card reader.
*/
@Override
protected String doInBackground(EpayData... params) {
     //your code here
   return null;  //<< return string from here
}
}

相关内容

  • 没有找到相关文章

最新更新