从异步任务中获取结果

编程入门 行业动态 更新时间:2024-10-10 11:22:14
本文介绍了从异步任务中获取结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想从异步任务中获取结果.如果我使用 task.execute.get,我的 UI 将被冻结.我希望我的异步任务将是独立的类,所以我不想将我的结果处理代码放在 onPostExecute 中.我在这里找到了一些关于从异步任务回调数据的信息:blog.evoxmusic.fr/content/how-implement-callback-asynctask在这里:android asynctask 向 ui 发送回调但问题是:1-我不知道什么时候处理结果?2-为什么要使用接口?3-使用接口与简单地将结果放在异步任务中的公共字段中与 onPostExecute 有什么区别?

I want to get my result from an Async task. If I use task.execute.get, my UI will be frozen. I want my Async task will be stand alone class so I don't want to put my result processing code in onPostExecute. I've found some information about call back data from Async task here: blog.evoxmusic.fr/content/how-implement-callback-asynctask and here: android asynctask sending callbacks to ui but the problem is: 1-I don't know when to process the result? 2-why to use interface? 3-What's the differences of using an interface with simply putting the result in a public field in Async task from onPostExecute?

这是我的异步类:

public class AsyncCallWs extends AsyncTask<String, Void, String> { private ProgressDialog dialog; public String methodName=""; private WebService ws; private ArrayList<ServiceParam> paramsList; private boolean hasParams; public AsyncCallWs(Activity activity,String methodName) { xLog.position(); try { this.dialog = new ProgressDialog(activity); this.methodName = methodName; hasParams = false; } catch (Exception e) { xLog.error(e.getMessage()); } } public AsyncCallWs(Activity activity,String methodName,ArrayList<ServiceParam> params) { xLog.position(); try { this.dialog = new ProgressDialog(activity); this.methodName = methodName; this.paramsList = params; hasParams = true; } catch (Exception e) { xLog.error(e.getMessage()); } } @Override protected void onPreExecute() { this.dialog.setMessage(PersianReshape.reshape("Loading...")); this.dialog.show(); } @Override protected String doInBackground(String... params) { xLog.position(); String result = "No async task result!"; try { ws = new WebService(PublicVariable.NAMESPACE, PublicVariable.URL); if (!hasParams){ result = ws.CallMethod(methodName); } else{ xLog.info("THIS METHOD IS: "+ methodName); result = ws.CallMethod(methodName,paramsList); xLog.info("THIS RESULT IS: "+ result); } } catch (Exception e) { xLog.error(e.getMessage()); } return result; } @Override protected void onPostExecute(String result) { xLog.position(); if (this.dialog.isShowing()) { this.dialog.dismiss(); } xLog.info("Output of current AsyncTask is:"+ result); } }

推荐答案

1-不知道什么时候处理结果?

1-I don't know when to process the result?

结果将在 onPostExecute 中处理,它反过来会在任何实现此接口的类中调用您的接口方法.因此,实际的 UI 内容将全部发生在您的 Activity 或 Fragment 或任何实现接口回调的内容中.您可以将任何数据传递给它.

The result will be processed in onPostExecute, which in turn will call your interface method in whatever class is implementing this interface. So the actual UI stuff will all take place in your Activity or Fragment or whatever is implementing the interface callback. You can pass any data you want to it.

2-为什么要使用接口?

2-why to use interface?

接口是将逻辑与 AsyncTask 和任何类(我假设是 Activity 或 Fragment)分离的好方法实施它.这也意味着任何实现这个接口的类都可以处理这个 AsyncTask 的结果,它变得很容易重用.

An interface is a great way to decouple the logic from your AsyncTask and whatever class (I assume an Activity or Fragment) that is implementing it. Also this means that any class that implements this interface can process results from this AsyncTask, it become easily re-usable.

3-使用接口与简单地将结果放在异步任务中的公共字段中与 onPostExecute 有什么区别?

3-What's the differences of using an interface with simply putting the result in a public field in Async task from onPostExecute?

您仍然不会收到回调 - 您的 Activity 或 Fragment 如何知道何时填充此字段并准备好接受询问?

You still won't get a callback - how will your Activity or Fragment know when this field is populated and ready to be interrogated?

更多推荐

从异步任务中获取结果

本文发布于:2023-11-24 11:53:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1625098.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!