Android asynctask的OnPostExecute()不是第一次调用

Android asynctask的OnPostExecute()不是第一次调用,android,android-asynctask,Android,Android Asynctask,我正在应用程序中使用AsyncTask。但是第一次onPostExecute没有被调用。从那以后,下一次它的召唤。有什么问题吗 这就是我调用AsyncTask的方式- new NotificationListAsyncTask(this, notificationCountHandler).execute(); import java.io.IOException; import java.io.UnsupportedEncodingException; import org.apache.

我正在应用程序中使用AsyncTask。但是第一次onPostExecute没有被调用。从那以后,下一次它的召唤。有什么问题吗

这就是我调用AsyncTask的方式-

new NotificationListAsyncTask(this, notificationCountHandler).execute();
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.SharedPreferences;

public class NotificationListAsyncTask extends AsyncTask<String, Void, Void> {
Context context;
private static final String TAG_DATA = "data";
private Handler handler;
public static final String KEY_NOTIFICATION_COUNT = "key_notification_count";
public static final int KEY_NOTIFICATION_MESSAGE = 1001;
private static final String TAG_NOTIFICATION_ID = "ID";

public NotificationListAsyncTask(Context context, Handler handler) {
    this.context = context;
    this.handler = handler;
}

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

@Override
protected Void doInBackground(String... params) {
    // Creating service handler class instance
    RequestHandler requestHandler = new RequestHandler(context);
    UrlBuilder builder =AppService
            .getAppService().getUrlBuilder();
    // Making a request to url and getting response
    String jsonStr = null;
    try {
        jsonStr = requestHandler.makeServiceCall(
                builder.getNotificationListUrl(
                        CommonFunctions.getuserId(context),
                        CommonFunctions.getUtcTime(context)),
                RequestHandler.GET);
    } catch (UnsupportedEncodingException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (ClientProtocolException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    if (jsonStr != null) {
        try {
            JSONObject jsonObj = new JSONObject(jsonStr);
            JSONArray contents = null;
            // Getting JSON Array node
            JSONArray contentsEnclosed = jsonObj.getJSONArray(TAG_DATA);
            contents = contentsEnclosed.getJSONArray(0);
            for (int i = 0; i < contents.length(); i++) {
                JSONObject obj = contents.getJSONObject(i);
                String jsonValue = obj.toString();
                String contentId = obj.getString(TAG_NOTIFICATION_ID);
                AppService.getAppService().insertNotification(jsonValue, contentId);
            }
            CommonFunctions.setNotificationrequestTime(context);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    } else {
        Log.e("RequestHandler", "Couldn't get any data from the url");
    }
    return null;
}

@Override
protected void onPostExecute(Void result) {
    super.onPostExecute(result);        
    handler.sendEmptyMessage(KEY_NOTIFICATION_MESSAGE);
}
}
下面是我的任务-

new NotificationListAsyncTask(this, notificationCountHandler).execute();
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.SharedPreferences;

public class NotificationListAsyncTask extends AsyncTask<String, Void, Void> {
Context context;
private static final String TAG_DATA = "data";
private Handler handler;
public static final String KEY_NOTIFICATION_COUNT = "key_notification_count";
public static final int KEY_NOTIFICATION_MESSAGE = 1001;
private static final String TAG_NOTIFICATION_ID = "ID";

public NotificationListAsyncTask(Context context, Handler handler) {
    this.context = context;
    this.handler = handler;
}

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

@Override
protected Void doInBackground(String... params) {
    // Creating service handler class instance
    RequestHandler requestHandler = new RequestHandler(context);
    UrlBuilder builder =AppService
            .getAppService().getUrlBuilder();
    // Making a request to url and getting response
    String jsonStr = null;
    try {
        jsonStr = requestHandler.makeServiceCall(
                builder.getNotificationListUrl(
                        CommonFunctions.getuserId(context),
                        CommonFunctions.getUtcTime(context)),
                RequestHandler.GET);
    } catch (UnsupportedEncodingException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (ClientProtocolException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    if (jsonStr != null) {
        try {
            JSONObject jsonObj = new JSONObject(jsonStr);
            JSONArray contents = null;
            // Getting JSON Array node
            JSONArray contentsEnclosed = jsonObj.getJSONArray(TAG_DATA);
            contents = contentsEnclosed.getJSONArray(0);
            for (int i = 0; i < contents.length(); i++) {
                JSONObject obj = contents.getJSONObject(i);
                String jsonValue = obj.toString();
                String contentId = obj.getString(TAG_NOTIFICATION_ID);
                AppService.getAppService().insertNotification(jsonValue, contentId);
            }
            CommonFunctions.setNotificationrequestTime(context);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    } else {
        Log.e("RequestHandler", "Couldn't get any data from the url");
    }
    return null;
}

@Override
protected void onPostExecute(Void result) {
    super.onPostExecute(result);        
    handler.sendEmptyMessage(KEY_NOTIFICATION_MESSAGE);
}
}

提前感谢。

请参阅AsynkTask生命周期:

onPreExecute,在执行任务之前在UI线程上调用。此步骤通常用于设置任务,例如在用户界面中显示进度条

doInBackgroundParams…,在onPreExecute完成执行后立即在后台线程上调用。此步骤用于执行可能需要很长时间的背景计算。异步任务的参数被传递到此步骤。此步骤必须返回计算结果,并将返回到最后一步

三,。OnPostExecuteSult,在后台计算完成后在UI线程上调用。背景计算的结果作为参数传递到此步骤

所以一旦doInBackground完成,onPostExecute方法将调用


请参考

我得到了解决方案。在应用程序类oncreate方法中使用以下代码-

@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
    try {
        Class.forName("android.os.AsyncTask");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

}

它对我来说很有效,但我仍然不知道为什么需要它,或者它是正确的方式。以前所有的异步任务都工作得很好。我不知道发生了什么事,因为它是从这样的行为开始的,现在它第一次不起作用了。如果有人知道,请解释

向我们展示了如何调用asynctask和asynctask代码。我也不知道,这就是为什么我要求它。这根本不是必需的。如果doInbackground执行罚款,它应该会起作用。阅读文档。