Android的AsyncTask的:多参数,可以返回值,等待 [英] Android's AsyncTask: multiple params, returning values, waiting

查看:208
本文介绍了Android的AsyncTask的:多参数,可以返回值,等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找如何,现在实施了一段时间Android的AsyncTask的类。基本上,我希望它做的经典场景:当我做一个网络调用简单动画无限期负重轮。然而,在实践中我在得到这个工作,我想要的方式有些问题。

I have been looking at how to implement Android's AsyncTask class for some time now. Basically, I want it to do the classic scenario: simply animate an indefinite loading wheel while I make a network call. However, in practice I'm having some issues getting this to work the way I want.

我有code,看起来像这样的目前:(之前我期望的AsyncTask实现)

I have code that looks like this currently: (before my desired AsyncTask implementation)

 String result = timeConsumingNetworkCall(String someData, ArrayList stuff1);
 doStuff(result);

 String result = timeConsumingNetworkCall(String otherData, ArrayList stuff2);
 doOtherStuff(result);

 String result = timeConsumingNetworkCall(String dataAgain, ArrayList stuff3);
 doYetEvenMore(result);

我希望能够重用我的AsyncTask通话将数据传递给它,它使一个网络呼叫,然后返回我的数据,我就可以处理,因为我想,像我上面做的。

I want to be able to reuse my AsyncTask call to pass data to it, it makes a network call, and then returns me data that I can then process as I wish, like I do above.

现在 - 这个完美的作品,当然除了应用程序出现挂起,没有加载。在谈到AsyncTask的。也许我只是不很明白 - 但是我怎么返回的AsyncTask的数据外,使 doStuffToUI()等,可以使用它呢?

Now - this works perfect, except of course the app appears to "hang" with no loading. In comes AsyncTask. Maybe I just don't quite "get it" yet - but how do I return data OUTSIDE of the AsyncTask so that doStuffToUI() etc can use it?

最后,我想,如果我可以使用类似于此的AsyncTask:

Ultimately, I'd like it if I could use the AsyncTask similar to this:

String result = new PostToFile("function_name", keysAndValues).execute().getResult();
doStuff(result); // this shouldn't execute until the AsyncTask is done

我要对这个权利?请问这是怎么应当/可以做什么?这里是我下面的AsyncTask的班,缩短为简洁。

Am I going about this right? Is this how it should be/can be done? Here's my AsyncTask class below, shortened for brevity.

private class PostToFile extends AsyncTask<PostToFile, Void, Void>{
    private String functionName;
    private ArrayList<NameValuePair> postKeyValuePairs;
    private String result = "";

    public PostToFile(String function, ArrayList<NameValuePair> keyValuePairs){
        functionName= function;
        postKeyValuePairs = keyValuePairs;
    }

    @Override
    protected void onPreExecute() {
        progressDialog = ProgressDialog.show(BaseActivity.getInstance(), "Loading", "Please wait...", true, false);
    }

    @Override
    protected Void doInBackground(PostToFile... params) {                       
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair(FUNCTION_KEYWORD, functionName));
        for (int i = 0; i < postKeyValuePairs.size(); i++)            {
            nameValuePairs.add(postKeyValuePairs.get(i));
        }

        try{
            // ***do the POST magic.***
            result = response.toString();
        }
        catch (Exception e){
             // clean up my mess
        }

        return null;
    }

    private String getResult(){
        return result; // can I use this somehow???
    }

    @Override
    protected void onPostExecute(Void result) {
        progressDialog.dismiss();
    }
 }

任何帮助是AP preciated,非常感谢在前进。我很高兴,如果需要澄清。

Any help is appreciated, thanks a lot in advance. I'm happy to clarify if needed.

推荐答案

请可重复使用的异步任务默认onPostExecute实现,然后覆盖在每次调用默认的实现:

Make a reusable async task with default onPostExecute implementation and then override default implementation for every call:

public static class PostToFile extends AsyncTask<Void, Void, String> {
        private String functionName;
        private ArrayList<NameValuePair> postKeyValuePairs;
        private String result = "";

        public PostToFile(String function, ArrayList<NameValuePair> keyValuePairs){
            functionName= function;
            postKeyValuePairs = keyValuePairs;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // show progress bar
        }

        @Override
        protected String doInBackground(Void... arg0) {
            // make request and get the result
            return null; // return result
        }

        @Override
        protected void onCancelled(String result) {
            super.onCancelled(result);
            // hide progress bar
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            // hide progress bar
        }
    }

执行异步的任务:

Execute your async task:

new PostToFile(function, keyValuePairs) {

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result); // this will hide a progress bar

            doStuff(result); 
        }
}.execute();

new PostToFile(otherFunction, otherKeyValuePairs) {

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result); // this will hide a progress bar

            doOtherStuff(result); 
        }
}.execute();

这篇关于Android的AsyncTask的:多参数,可以返回值,等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆