![](/img/trans.png)
[英]Asynctask: pass values from doInBackground to onPostExecute
[英]AsyncTask: Pass an exception value from doInBackground to onPostExecute
我试图将我的doInBackground之间的错误消息传递给onPostExecute。 有没有办法使用数据结构来做到这一点? 在我的情况下,我正在尝试避免创建一个类并返回一个对象
@Override
protected User doInBackground(List<Pair<String, String>>... params) {
randomAPI api = randomAPI
.getInstance(context);
try {
**thing that will generate error**
}
} catch (RandomException e) {
Log.e(TAG, "Error", e);
}
return somethingThatIsUsableInOnPostExecute;
}
@Override
protected void onPostExecute(User result) {
}
private String exception = "";
@Override
protected User doInBackground(List<Pair<String, String>>... params) {
randomAPI api = randomAPI
.getInstance(context);
try {
**thing that will generate error**
}
} catch (RandomException e) {
Log.e(TAG, "Error", e);
exception = e.getMessage();
}
return somethingThatIsUsableInOnPostExecute;
}
@Override
protected void onPostExecute(User result) {
if(!exception.isEmpty()){
//exception path
}
}
您应该尝试将
doInBackground
return
类型从User
对象更改为Object
并将其onPostExecute
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.