Android上的onPostExect AsyncTask没有被调用

时间:2017-06-12 07:37:36

标签: java android android-asynctask alertdialog

我尝试通过AsyncTask创建一个Android客户端。服务器具有要为同一连接执行的主要和辅助作业。如果主要部分返回一些结果,那么我不需要做次要部分,但如果服务器返回null,我想要用户使用AlertDialog,如果他想继续,如果他说是,那么我继续与服务器通信

为此,我已经在doInBackground()和onPostExecute()方法上分离了调用。这是我的逻辑:

@Override
protected Object doInBackground(Object... params) {
    //Do primary part of the call
    return obj;
}

@Override
protected void onPostExecute(Object obj) {
    if(obj == null) {
        super.onPostExecute(obj);
        //Throw alert window to ask user if he wants to continue
        //If user says yes call server to do the the secondary part
    }
}
当我调用这个AyncTask时,来自mainActivity的

我使用了get()方法,这样我就可以等到它消失后才能得到结果。 我的问题是onPostExecute()永远不会被调用...任何想法?它是否扮演任何角色我在onPostExecute()上调用服务器的事实?

1 个答案:

答案 0 :(得分:0)

ASYNCTASK的示例用途

想试试AsyncTask吗?以下是如何使用此功能的示例。

class DoSomeTask extends AsyncTask{

@Override
protected void onPreExecute() {
      //Setup precondition to execute some task 
}

@Override 
protected String doInBackground(String... params) {
     //Do some task
     publishProgress (1);
     return ""; 
}

@Override
protected void onProgressUpdate(Integer... values) { 
     //Update the progress of current task 
}

@Override 
protected void onPostExecute(String s) { 
     //Show the result obtained from doInBackground
}
}

更多详情:https://www.upwork.com/hiring/mobile/why-you-should-use-asynctask-in-android-development/

http://www.journaldev.com/9708/android-asynctask-example-tutorial