从异步任务中获取结果 [英] Get the result from async task
问题描述
我想从异步任务中获取结果.如果我使用 task.execute.get,我的 UI 将被冻结.我希望我的异步任务将是独立的类,所以我不想将我的结果处理代码放在 onPostExecute 中.我在这里找到了一些关于从异步任务回调数据的信息:http://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: http://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?
这篇关于从异步任务中获取结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!