这个asyncTask类在Launcher Activity的onCreate()方法中调用,我想从这个2 AsyncTask类调用服务并在sqlite数据库中存储数据。但是在这两个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();
}
}
}
}
在这里有两个不兼容的AsyncTask,我想先执行AsyncTask而不是执行其他任务。如何实施
答案 0 :(得分:1)
将super.onPostExecute(response);
放在protected void onPostExecute(String response)
的底部,在代码行之后。
他们的方法也是super.onPreExecute();
和super.onCancelled(result);
。
答案 1 :(得分:0)
也许你的http请求不返回结果?尝试doInBackground
在没有互联网互动的情况下返回忠诚的结果。
关于两个AsyncTask:
如果您真的想要并行执行,可以调用 executeOnExecutor(java.util.concurrent.Executor,Object [])with THREAD_POOL_EXECUTOR。
从这里http://developer.android.com/reference/android/os/AsyncTask.html