AsyncTask的onPostExecute()不会被调用+ W /艺术:暂停所有线程了:1.1ms [英] AsyncTask onPostExecute() is never called + W/art: Suspending all threads took: 1.1ms

查看:340
本文介绍了AsyncTask的onPostExecute()不会被调用+ W /艺术:暂停所有线程了:1.1ms的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有很多的的AsyncTask 这个问题,但没有发生的事情来帮助我。

I know there a lot of AsyncTask questions about this but none happened to help me.

我得到了一个的AsyncTask 处理使用 Jsoup <一些 HTML 解析/ code>。

I got an AsyncTask that handles some HTML parsing using Jsoup.

我调试的应用程序无数次(真的),并在 doInBackground()方法调用我得到了我想要送 onPostExecute值() 方法,但问题的 onPostExecute()方法不会被调用。

I debugged the app countless times(really) and after doInBackground() method called i get the values i want to send to onPostExecute() method but the problem the onPostExecute() method is never called.

我想这是通过我滑倒简单的东西。

I guess it's something simple that slip through me.

只是相关code。

MainActivity.java:

private HtmlPage htmlPage = new HtmlPage();

    private static final String ASYNC_TASK_TAG = "AsyncTask";

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Getting the HTML document from a background thread
        new GetHtmlDocument("someUrlString").execute();

        if (this.htmlPage.getHtmlDocument() != null)
        {
            new GetHtmlDocument("someUrlString").cancel(true);
        }

        while (this.htmlPage.getHtmlDocument() == null)
        {
            if (this.htmlPage.getHtmlDocument() != null)
            {
                new GetHtmlDocument("someUrlString").cancel(true);
            }
            else
            {
                new GetHtmlDocument("someUrlString").execute();
            }
        }
    }

AsyncTask的内部类:

private class GetHtmlDocument extends AsyncTask<String,Void,HtmlPage>
    {
        private String url;

        /**
         * Constructor.
         *
         * @param url Url to parse from in the web.
         */
        public GetHtmlDocument(String url)
        {
            this.url = url;
        }

        @Override
        protected void onPreExecute()
        {
            Log.d(MainActivity.ASYNC_TASK_TAG, "onPreExecute() called");
        }

        @Override
        protected HtmlPage doInBackground(String... params)
        {
            //android.os.Debug.waitForDebugger();
            Log.d(MainActivity.ASYNC_TASK_TAG, "doInBackground() called");

            // Get the HTML http://www.lyricsplanet.com/ document
            HtmlPage htmlPage = new HtmlPage(getParsedDocument(this.url));

            return htmlPage;
        }

        @Override
        protected void onPostExecute(HtmlPage htmlPage)
        {
            Log.d(MainActivity.ASYNC_TASK_TAG, "onPostExecute() called");

            if (htmlPage.getHtmlDocument() != null)
            {
                this.cancel(true);
            }

            setHtmlPage(htmlPage);
        }

        /**
         * A task can be cancelled at any time by invoking cancel(boolean).
         * Invoking this method will cause subsequent calls to isCancelled() to return true.
         * After invoking this method, onCancelled(Object), instead of onPostExecute(Object) will be invoked after doInBackground(Object[]) returns.
         * To ensure that a task is cancelled as quickly as possible, you should always check the return value of isCancelled() periodically from doInBackground(Object[]), if possible (inside a loop for instance.)
         *
         * @param htmlPage
         *
         */
        @Override
        protected void onCancelled(HtmlPage htmlPage)
        {
            Log.d(MainActivity.ASYNC_TASK_TAG, "onCancelled() called");
        }
    }

法onPostExecute()使用:

public void setHtmlPage(HtmlPage htmlPage)
    {
        this.htmlPage = htmlPage;
    }

HtmlPage.java:

public class HtmlPage
{
    private Document htmlDocument;

    public HtmlPage(Document htmlDocument)
    {
        this.htmlDocument = htmlDocument;
    }
}

方法,在doInBackground()使用的:

   public Document getParsedDocument(String url)
    {
        try
        {
            return Jsoup.connect(url).get();
        }
        catch (IOException e) // On error
        {
            e.printStackTrace();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return null; // Failed to connect to the url
    }

我想这个问题是与主,从来没有得到一个机会来调用 onPostExecute()方法或一些与的通用参数我的AsyncTask 或与我实现我的的AsyncTask 类。

I guess the problem is with the main Thread that never gets a chance to call onPostExecute() method or something with the Generic parameters of my AsyncTask or something with my implementation of my AsyncTask class.

任何建议将是非常美联社preciated。

Any suggestions will be very appreciated.

编辑:

我忘了 W /艺术:暂停所有线程了:1.1ms 警告。我得到运行的应用程序。但是,当我调试应用程序,我不明白这一点,我清楚地看到),该公司在 doInBackground(已生成的 HtmlPage 对象方法得到我想要的值。

I forgot about the W/art: Suspending all threads took: 1.1ms warning. I get that for running the app. But when i debug the app i don't get that and i clearly see that the HtmlPage object that's been generated in the doInBackground() method get the value i want.

推荐答案

就叫新GetHtmlDocument(someUrlString)执行(); 并等待导致onPostExecute ()。没有必要while循环使用(即阻止你的UI线程),或致电取消()(实际上它不会做在默认情况下任何东西,在底部检查链接)。

Just call new GetHtmlDocument("someUrlString").execute(); and wait result in the onPostExecute(). There is no need to use while loop (that blocks your UI thread) or to call cancel() (actually it doesn't do anything by default, check link at the bottom).

请注意,

AsyncTask的应仅用于工作/这需要相当长几秒钟的操作;

AsyncTask should only be used for tasks/operations that take quite few seconds;

AsyncTasks在单个后台线程(从API​​ 11)串行执行。所以,长时间运行的工作人员可以阻止他人;

AsyncTasks are executed serially on a single background thread (from API 11). So long running worker can block others;

其他的一些陷阱的。

这篇关于AsyncTask的onPostExecute()不会被调用+ W /艺术:暂停所有线程了:1.1ms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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