Java AsyncTask何时onPostExecute()未调用?
我试图确保我考虑了所有可能的情况,以避免用户出错 我有一个AsyncTask,它实现了一个接口,并在AsyncTask的onPostExecute中返回对调用类的响应,如下所示:Java AsyncTask何时onPostExecute()未调用?,java,android,android-asynctask,Java,Android,Android Asynctask,我试图确保我考虑了所有可能的情况,以避免用户出错 我有一个AsyncTask,它实现了一个接口,并在AsyncTask的onPostExecute中返回对调用类的响应,如下所示: public class GetVideoInfoFromDataBase extends AsyncTask { Context context; private AsyncInterface asyncInterface; // Paginated list of results for
public class GetVideoInfoFromDataBase extends AsyncTask {
Context context;
private AsyncInterface asyncInterface;
// Paginated list of results for alarm database scan
static PaginatedScanList<AlarmDynamoMappingAdapter> results;
// The DynamoDB object mapper for accessing DynamoDB.
private final DynamoDBMapper mapper;
public GetVideoInfoFromDataBase(Context context, AsyncInterface asyncInterface){
mapper = AWSMobileClient.defaultMobileClient().getDynamoDBMapper();
this.context = context;
this.asyncInterface = asyncInterface;
}
@Override
protected Object doInBackground(Object[] params) {
System.out.println("The backgrond stuff is working 1");
DynamoDBScanExpression scanExpression = new DynamoDBScanExpression();
results = mapper.scan(AlarmDynamoMappingAdapter.class, scanExpression);
return results;
}
@Override
public void onPostExecute(Object obj) {
asyncInterface.response((PaginatedScanList<AlarmDynamoMappingAdapter> )obj);
}
}
公共类GetVideoInfoFromDataBase扩展异步任务{
语境;
专用异步接口;
//报警数据库扫描结果的分页列表
静态分页数据扫描列表结果;
//用于访问DynamoDB的DynamoDB对象映射器。
私人最终发电机映射器;
公共GetVideoInfoFromDataBase(上下文上下文,异步接口){
mapper=AWSMobileClient.defaultMobileClient().getDynamoDBMapper();
this.context=上下文;
this.asyncInterface=asyncInterface;
}
@凌驾
受保护对象doInBackground(对象[]参数){
System.out.println(“后台工作1”);
DynamoDBScanExpression scanExpression=新的DynamoDBScanExpression();
结果=mapper.scan(AlarmDynamoMappingAdapter.class,scanExpression);
返回结果;
}
@凌驾
公共void onPostExecute(对象obj){
asyncInterface.response((PaginatedScanList)obj);
}
}
是否有任何时候onPostExecute不会被调用,这意味着接口不会返回值
提前感谢您的帮助。onPostExecute()
只要doInBackground()
返回并且您没有明确取消任务(通过对AsyncTask
实例调用cancel()
),就会运行
从技术上讲,onPostExecute()
将在(如果)成功执行以下行后被调用:
return results;
只要您的进程在此期间没有被终止
如果已取消任务,将调用
onCancelled()
,而不是onPostExecute()
应调用onPostExecute
。为什么不System.out.println
在onpostExecute
中输入一些内容,以确保它是否调用了。它调用得很好。我想问的是,是否有任何情况会导致它不被调用。onpostExecute
方法在任务取消时不会被调用。在这种情况下,onCancelled(Object)
在doInBackground(Object[])
返回后在UI线程上被调用。谢谢,所以如果我的数据库调用失败,它仍将以空值运行?假设我添加一个try-catch并捕获异常。