Android AsyncTask获取块doInBackground

时间:2014-08-05 14:49:05

标签: android android-asynctask

我正在做一个AsyncTask。如果我做了一个get,那么doInBackground永远不会执行它。

我的代码:

private boolean registroServidor(String usuario, String regId, String pass)
{
    Toast toast1;

    context = getApplicationContext();

    mAuthTask=new UserLoginTask();
    mAuthTask.execute();

    try {
        if(!mAuthTask.get(5000,TimeUnit.MILLISECONDS)){
            toast1=Toast.makeText(getApplicationContext(),"Error al conectarse", Toast.LENGTH_SHORT);
            toast1.show();
            return false;
        }else{
            toast1=Toast.makeText(getApplicationContext(),"bien conectado", Toast.LENGTH_SHORT);
            toast1.show();
            conectado=true;
            mAuthTask.cancel(true);
            return true;
        }
    } catch (InterruptedException e) {

        e.printStackTrace();
        return false;
    } catch (ExecutionException e) {

        e.printStackTrace();
        return false;
    } catch (TimeoutException e) {

        e.printStackTrace();
        return false;
    }

}

public class UserLoginTask extends AsyncTask<Void, Void, Boolean> {
    private GestorCredencial credenciales = null;



    @Override
    protected Boolean doInBackground(Void... params) {
        this.credenciales=new GestorCredencial(user,pass);
        boolean res=credenciales.enviar();
        return res;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }



    @Override
    protected void onPostExecute(final Boolean success) {
        mAuthTask = null;
    }

    @Override
    protected void onCancelled() {
        mAuthTask = null;
    }


}

logcat的

08-05 17:12:53.843: W/System.err(3292): java.util.concurrent.TimeoutException
08-05 17:12:53.882: W/System.err(3292):     at java.util.concurrent.FutureTask.get(FutureTask.java:173)
08-05 17:12:53.883: W/System.err(3292):     at android.os.AsyncTask.get(AsyncTask.java:502)
08-05 17:12:53.883: W/System.err(3292):     at com.laguna.talkme.gui.GUIPanelPrincipal.registroServidor(GUIPanelPrincipal.java:617)
08-05 17:12:53.883: W/System.err(3292):     at com.laguna.talkme.gui.GUIPanelPrincipal.access$11(GUIPanelPrincipal.java:605)
08-05 17:12:53.885: W/System.err(3292):     at com.laguna.talkme.gui.GUIPanelPrincipal$TareaRegistroGCM.doInBackground(GUIPanelPrincipal.java:571)
08-05 17:12:53.885: W/System.err(3292):     at com.laguna.talkme.gui.GUIPanelPrincipal$TareaRegistroGCM.doInBackground(GUIPanelPrincipal.java:1)
08-05 17:12:53.885: W/System.err(3292):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
08-05 17:12:53.885: W/System.err(3292):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
08-05 17:12:53.885: W/System.err(3292):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
08-05 17:12:53.885: W/System.err(3292):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
08-05 17:12:53.885: W/System.err(3292):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
08-05 17:12:53.885: W/System.err(3292):     at java.lang.Thread.run(Thread.java:838)

问题:我执行execute(),然后调试,然后运行onpreExecute,但不是doinBackground()。当5秒超时运行doinbackground时。我得到一个TimeException:S

有什么建议吗?

感谢并抱歉我的英语不好

2 个答案:

答案 0 :(得分:0)

尝试将以下代码放在onPostExecute()中,看看它是否有效..

  try {
         if(!mAuthTask.get(5000,TimeUnit.MILLISECONDS)){
             toast1=Toast.makeText(getApplicationContext(),"Error al conectarse", Toast.LENGTH_SHORT);
             toast1.show();
             return false;
         }else{
             toast1=Toast.makeText(getApplicationContext(),"bien conectado", Toast.LENGTH_SHORT);
             toast1.show();
             conectado=true;
             mAuthTask.cancel(true);
             return true;
         }
     } catch (InterruptedException e) {

         e.printStackTrace();
         return false;
     } catch (ExecutionException e) {

         e.printStackTrace();
         return false;
     } catch (TimeoutException e) {

         e.printStackTrace();
         return false;
     }

而不是mAuthTask.get(5000,TimeUnit.MILLISECONDS)使用onPostExecute()'成功'参数

答案 1 :(得分:0)

默认情况下,AsyncTask在特殊线程中按顺序执行。为了使执行并行,您需要使用:

new UserLoginTask().executeOnExecutor( AsyncTask.THREAD_POOL_EXECUTOR );

更新:

你不应该调用get(),因为它会破坏整个异步模式。

这一行

  

this.credenciales = new GestorCredencial(用户,通行证);

应该放在构造函数