一个接一个Android的多个异步任务

时间:2014-11-10 10:27:51

标签: android android-asynctask

我试图通过Splash上​​的SQLite保存在Async task数据库中从服务器获取数据。我在服务器上有多个表,需要一个接一个地获取。我试着这样做

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);

    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork()
            .penaltyLog().build());

    url = getResources().getString(R.string.url);

    db = new SQLCont(context);

    new asyn_Task1(Splash.this).execute();

}

public class asyn_Task1 extends AsyncTask<String, Void, Boolean> {

    public asyn_Task1(Splash activiy) {
        context = activiy;
    }

    @Override
    protected void onPostExecute(Boolean result) {

        super.onPostExecute(result);

        new asyn_Task2(Splash.this).execute();

    }

    @Override
    protected void onPreExecute() {

        super.onPreExecute();

        progressdialog = new ProgressDialog(Splash.this);
        progressdialog.setTitle("Processing....");
        progressdialog.setMessage("Please Wait.....1 /10");
        progressdialog.setCancelable(false);
        progressdialog.show();
    }

    @Override
    protected Boolean doInBackground(String... params) {


        postParameters.add(new BasicNameValuePair("001", data));

        try {

            CustomHttpClient.executeHttpGet("001");

        } catch (Exception e1) {

            e1.printStackTrace();
        }

        String response = null;

        // call executeHttpPost method passing necessary parameters

        try {

            response = CustomHttpClient.executeHttpPost(

            url,

            postParameters);

            // store the result returned by PHP script that runs
            // MySQL query

            String result = response.toString();

            // parse json data
            try {

                JSONArray jArray = new JSONArray(result);

                for (int i = 0; i < jArray.length(); i++) {

                    JSONObject json_data = jArray.getJSONObject(i);

                    id = json_data.getString("id");
                    st_name = " " + json_data.getString("name");
                    st_contact = json_data.getString("contact");
                    st_category = json_data.getString("Category");
                    st_address = json_data.getString("address");

                    Log.d("favourite_data", "" + id + st_name + st_contact
                            + st_category + st_address);

                    db.adddata_hospital(context, st_name,st_contact,
                            st_category, st_address);


                }

            } catch (JSONException e) {

                Log.e("log_tag", "Error parsing data " + e.toString());
            }

        } catch (Exception e) {

            Log.e("log_tag", "Error in http connection!!" + e.toString());

        }
        return null;
    }
}

public class asyn_Task2 extends AsyncTask<String, Void, Boolean> {

    public asyn_Task2(Splash activiy) {
        context = activiy;
    }

    @Override
    protected void onPostExecute(Boolean result) {

        super.onPostExecute(result);

        new asyn_Task3(Splash.this).execute();

    }

    @Override
    protected void onPreExecute() {

        super.onPreExecute();


    }

    @Override
    protected Boolean doInBackground(String... params) {

        // some stuff here

        }
        return null;
    }
}

public class asyn_Task3 extends AsyncTask<String, Void, Boolean> {

    public asyn_blood_Group(Splash activiy) {
        context = activiy;
    }

    @Override
    protected void onPostExecute(Boolean result) {

        super.onPostExecute(result);
        progressdialog.dismiss();


    }

    @Override
    protected void onPreExecute() {

        super.onPreExecute();

    }

    @Override
    protected Boolean doInBackground(String... params) {

      // some stuff here
        }
        return null;
    }
}

问题是每次重复asyn_Task1都会添加数据

预期结果

  abc  def  ghi 

  jkl  mno  pqr 

  mno  pqr   stu

但获得输出

 abc  def  ghi

 abc  def  ghi

 abc  def  ghi

1 个答案:

答案 0 :(得分:0)

您将根据查询获取数据。如果您始终执行相同的查询,那么您将始终获得相同的数据。

从我的角度来看,如果可能的话就是单一的AsyncTask。当所有查询完成后,将触发onpostExecute()回调。