English 中文(简体)
Asynctask Error Handling
原标题:Asynctask Error Handling

我正在使用<条码>AsyncTask进行某些背景计算,但我无法找到处理例外情况的正确办法。 目前,我使用以下法典:

private class MyTask extends AsyncTask<String, Void, String>
{
    private int e = 0;

    @Override
    protected String doInBackground(String... params)
    {
        try
        {
            URL url = new URL("http://www.example.com/");
        }
        catch (MalformedURLException e)
        {
            e = 1;
        }

        // Other code here...

        return null;
    }

    @Override
    protected void onPostExecute(String result)
    {
        if (e == 1)
            Log.i("Some Tag", "An error occurred.");

        // Perform post processing here...
    }
}

我认为,变数(e)可由主线和工人线书写/使用。 我知道,onPostExecute() 只有在填写了do InBackround( 之后才能运行,我能否放弃任何同步?

这种坏的法典吗? 在<条码>AyncTask中是否有处理例外情况的商定或正确方式?

问题回答

在我看来,我不认为有更好的办法。

页: 1 Murphy response about it.

我认为,您的法典将适用于这项工作,但已经在以下几类错误处理:AsyncTask

http://developer.android.com/ reference/android/os/AsyncTask.html#cancel%28boolean%29”rel=“nofollow noreferer”>cancel() 方法及其手勒方法Cancelled.。 当你在do InBackground()中,在国联线深处采用了Cancelled方法。 您是否要求取消(真实)或取消(取消)取决于你们的需要。


private class MyTask extends AsyncTask<String, Void, String>
{    
    @Override
    protected NewsItem doInBackground(String... params)
    {
        try
        {
            URL url = new URL("http://www.example.com/");
        }
        catch (MalformedURLException e)
        {
            cancel(false/true);  
        }

        // Other code here...

        return null;
    }

    @Override
    protected void onPostExecute(String result)
    {              
        // Perform successful post processing here...
    }

   @Override
    protected void onCancelled() {
        super.onCancelled();
        // Perform error post processing here...
    }
}

即便在SMP结构方面也是如此。 所有同步工作都是为你进行的。 然而,最好利用回报价值来做到这一点。





相关问题
handling exceptions IN Action Filters

Is there a better way to handle exceptions that occur inside an Action Filter itself in ASP .NET MVC? There re 2 ways I can think of at the moment. Using a try catch and setting the HTTP Status ...

既可捕获,又可举出例外。

我有一种办法,可以进入亚洲开发银行,因此,我国的亚行在多瑙河航道中的所有 st子都位于一个试捕区。 它正在追捕Kexception

Cross compiler exception handling - Can it be done safely?

I am doing some maintenance on a C++ windows dll library that is required to work with different VC++ compilers (as I don’t want to address different mangling schemes). I have already eliminated any ...

File Handling Issue

I am developing a tool in c#, at one instance I start writing into a xml file continuously using my tool,when i suddenly restart my machine the particular xml file gets corrupted, what is the reason ...

Watch a memory location/install data breakpoint from code?

We have a memory overwrite problem. At some point, during the course of our program, a memory location is being overwritten and causing our program to crash. the problem happens only in release mode. ...

Unit Test for Exceptions Message

Is there a simple (Attribute-driven) way to have the following test fail on the message of the exception. [TestMethod()] [ExpectedException(typeof(ArgumentException))] public void ExceptionTestTest() ...

热门标签