1

这个 asyncTask 类在 Launcher Activity 的 onCreate() 方法中被调用,我想从这个 2 AsyncTask 类调用服务并将数据存储在 sqlite 数据库中。但是在这 2 个 asyncTask 中调用了 onPreExecute() 和 doInBackground() 方法,但从未调用过 onPostExecute() 方法。请告诉我为什么会这样,谢谢

public class EcafeHome extends Activity 
{
    @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);

            new GetOfferData(EcafeHome.this).execute("runnable");
            new GetMenuData(EcafeHome.this).execute("params");
        }
}

    public class GetOfferData extends AsyncTask<String, Void, String>
    {
        Context mContext = null;
        public GetOfferData(Context mContext)
        {
            this.mContext = mContext;
        }

        @Override
        protected void onPreExecute()
        {
            super.onPreExecute();
            Log.d("GetOfferJsonData", "onPreExecute");
        }

        @Override
        protected String doInBackground(String... params)
        {
            Log.d("GetOfferJsonData", "doInBackground");
            return Common.InputStreamToString(Common.HTTP_GET(Constant.host, Constant.serviceUrlProt, Constant.URL.catagoryList));
        }

        @Override
        protected void onCancelled(String result) 
        {
            super.onCancelled(result);
            Log.d("GetOfferJsonData", "onCancelled");
        }

        @Override
        protected void onPostExecute(String response) 
        {
            super.onPostExecute(response);
            Log.d("GetOfferJsonData", "onPostExecute");
            if(response != null)
            {
                Log.d("response", response);
                try 
                {
                    JSONObject mJsonObject = new JSONObject(response);
                    JSONArray jArrayPackages = mJsonObject.getJSONArray(Constant.JSON_KEY.PACKAGE.PACKAGES); 

                    for(int i = 0; i < jArrayPackages.length(); i++)
                    {
                        JSONObject jObjectPackages = jArrayPackages.getJSONObject(i);

                        int packageId = jObjectPackages.getInt(Constant.JSON_KEY.PACKAGE.ID);

                        String pkgName = jObjectPackages.getString(Constant.JSON_KEY.PACKAGE.NAME);

                        int pkgprice = jObjectPackages.getInt(Constant.JSON_KEY.PACKAGE.PRICE);

                        String pkgpic = jObjectPackages.getString(Constant.JSON_KEY.PACKAGE.PIC);

                        DBConstant.openDatabase(mContext);
                        Cursor cursorOffer = DBConstant.mDBHelper.getPackageInfo(DBConstant.mSQLiteDatabase, packageId); 
                        if(cursorOffer != null)
                        {
                            Log.d("package offer", ""+cursorOffer.getCount());
                            if(cursorOffer.getCount() == 0)
                            {
                                DBConstant.mDBHelper.insertPackageInfo(DBConstant.mSQLiteDatabase, packageId, pkgName, pkgprice, pkgpic);
                            }
                        }
                        else
                        {

                        }
                        if(!cursorOffer.isClosed())
                            cursorOffer.close();

                        JSONArray jArrayPackageItem = jObjectPackages.getJSONArray(Constant.JSON_KEY.PACKAGE.PACKAGE_ITEMS);

                        for(int j = 0; j < jArrayPackageItem.length(); j++) 
                        {
                            JSONObject jObjectPackageItem = jArrayPackageItem.getJSONObject(j);
                            int pkgItemID = jObjectPackageItem.getInt(Constant.JSON_KEY.PACKAGE_ITEM.ID);

                            int pkgItemQnty = jObjectPackageItem.getInt(Constant.JSON_KEY.PACKAGE_ITEM.QUANTITY);

                            int menuitemid = jObjectPackageItem.getInt(Constant.JSON_KEY.PACKAGE_ITEM.MENU_ITEM_ID);

                            int pkgID = jObjectPackageItem.getInt(Constant.JSON_KEY.PACKAGE_ITEM.PACKAGE_ID);

                            //DBConstant.mDBHelper.insertPackageItemInfo(DBConstant.mSQLiteDatabase, pkgItemID, pkgID, menuitemid, pkgItemQnty);
                            DBConstant.openDatabase(mContext);
                            Cursor cursorOfferItem = DBConstant.mDBHelper.getPackageItemInfo(DBConstant.mSQLiteDatabase, pkgItemID); 
                            if(cursorOfferItem != null)
                            {
                                Log.d("package Item offer", ""+cursorOfferItem.getCount());
                                if(cursorOfferItem.getCount() == 0)
                                {
                                    DBConstant.mDBHelper.insertPackageItemInfo(DBConstant.mSQLiteDatabase, pkgItemID, pkgID, menuitemid, pkgItemQnty);
                                }
                            }
                            if(!cursorOfferItem.isClosed())
                                cursorOfferItem.close();

                            DBConstant.closeDatabase(); 
                        }
                    }

                }
                catch (JSONException e) 
                {
                    e.printStackTrace();
                }
            }
        }
    }

在此有 2 个 AsyncTask 不能并行工作,我想先执行两个 AsyncTask,然后再执行其他任务。那么它是如何实现的

4

2 回答 2

1

将 放在super.onPostExecute(response);的底部protected void onPostExecute(String response),代码行之后。

还有他们super.onPreExecute();super.onCancelled(result);方法。

于 2013-02-08T09:49:16.480 回答
0

也许你的http请求不返回结果?尝试在doInBackground没有互联网交互的情况下获得许多结果。

关于两个 AsyncTask:

如果你真的想要并行执行,你可以使用 THREAD_POOL_EXECUTOR 调用 executeOnExecutor(java.util.concurrent.Executor, Object[])。

从这里http://developer.android.com/reference/android/os/AsyncTask.html

于 2013-02-08T10:03:48.420 回答