【发布时间】:2012-06-21 12:43:13
【问题描述】:
是否有特定的方法来处理 AsyncTask 中的故障?据我所知,唯一的方法是使用任务的返回值。如果可能,我希望能够提供有关失败的更多详细信息,并且 null 不是很冗长。
理想情况下,它会提供一个 onError 处理程序,但我认为它没有。
class DownloadAsyncTask extends AsyncTask<String, Void, String> {
/** this would be cool if it existed */
@Override
protected void onError(Exception ex) {
...
}
@Override
protected String doInBackground(String... params) {
try {
... download ...
} catch (IOException e) {
setError(e); // maybe like this?
}
}
}
【问题讨论】: