繁体   English   中英

AsyncTask:将异常值从doInBackground传递给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.

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