如何同时执行Asynctask

时间:2014-02-08 14:30:48

标签: android android-asynctask

我在一个班级有两个asynctask,我在同时执行它们时遇到了麻烦。 这是我的场景,我的用户点击视图1月日历按钮,它将显示日历月的事件及其评论。事件和注释存储在mysql数据库中,因此我必须获取不同的URL。我使用asyctask来获取url。每当我调用另一个asynctask它就无法解决。帮助我!

这是我的代码:

package sscr.stag;




@SuppressLint("ShowToast")
public class Calendar extends ListActivity {
// All xml labels

TextView txtEvent;


// Progress Dialog
private ProgressDialog pDialog;

JSONArray user;
JSONObject hay;
private static final String CALENDAR_URL = "http://www.stagconnect.com/StagConnect/calendar/januaryCalendar.php";
private static final String COMMENT_URL = "http://www.stagconnect.com/StagConnect/calendar/januaryReadComment.php";
private static final String TAG_USER = "username";
private static final String TAG_MESSAGE = "message";
private static final String TAG_TIME = "time";
private static final String TAG_ARRAY = "posts";
private JSONArray mCalendar = null;
private ArrayList<HashMap<String, String>> mCommentList;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.calendaradmin);

    txtEvent = (TextView) findViewById(R.id.event);

  new LoadCalendar().execute();
  new LoadCommentCal().execute(); // error here, says cannot be resolved in this type

}


private class LoadCalendar extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(Calendar.this);
        pDialog.setMessage("Loading Calendar.. ");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();

    }

    protected String doInBackground(String... args) {
        String json = null;


        try {

            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(CALENDAR_URL);
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity resEntity = response.getEntity();
            json = EntityUtils.toString(resEntity);

            Log.i("Profile JSON: ", json.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return json;
    }

    @Override
    protected void onPostExecute(String json) {
        super.onPostExecute(json);

        pDialog.dismiss();
        try
        {
        hay = new JSONObject(json);
        JSONArray user = hay.getJSONArray("posts");
        JSONObject jb= user.getJSONObject(0);
        String event = jb.getString("activities");  
        txtEvent.setText(event);
        }catch(Exception e)
        {
            e.printStackTrace();
        }

    }

        public void updateJSONdata() {
            mCommentList = new ArrayList<HashMap<String, String>>();
            JSONParser jParser = new JSONParser();
            JSONObject json = jParser.getJSONFromUrl(COMMENT_URL);

            try {       
                mCalendar = json.getJSONArray(TAG_ARRAY);
                for (int i = 0; i < mCalendar.length(); i++) {

                    JSONObject c = mCalendar.getJSONObject(i);              

                    String username = c.getString(TAG_USER);
                    HashMap<String, String> map = new HashMap<String, String>();


                    map.put(TAG_USER, username);


                    mCommentList.add(0, map);

                }   

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

        private void updateList() {
            ListAdapter adapter = new SimpleAdapter(Calendar.this, mCommentList,
                R.layout.calendar_comment, new String[] { TAG_MESSAGE,
                        TAG_USER, TAG_TIME }, new int[] { R.id.message,
                        R.id.username, R.id.time });


        setListAdapter(adapter);

        ListView lv = getListView();     
        lv.scrollTo(0, 0);      

        }

        public class LoadCommentCal extends AsyncTask<Void, Void, String> {

                @Override
                protected void onPreExecute() {
                    super.onPreExecute();
                    pDialog = new ProgressDialog(Calendar.this);
                    pDialog.setMessage("Loading Comments...");
                    pDialog.setIndeterminate(false);
                    pDialog.setCancelable(true);
                    pDialog.show();
                }

                @Override
                protected String doInBackground(Void... args) {
                    updateJSONdata();
                    return null;

                }



                @Override
                protected void onPostExecute(String result) {
                    super.onPostExecute(result);
                    pDialog.dismiss();
                    updateList();
                }

            }

}

}

2 个答案:

答案 0 :(得分:1)

你的第二个asynctask public class LoadCommentCal extends AsyncTask在第一个asynctask里面! 你需要把它从第一个asynctask移出。

private class LoadCalendar extends AsyncTask<String, String, String> {
...
}

... other functions

private class LoadCommentCal extends AsyncTask<Void, Void, String> {
...
}

答案 1 :(得分:0)

你在第一个AsyncTask中声明了很多东西我强烈建议在onPostExecute之后关闭你的AsyncTask

    @Override
    protected void onPostExecute(String json) {
        super.onPostExecute(json);

        pDialog.dismiss();
        try
        {
        hay = new JSONObject(json);
        JSONArray user = hay.getJSONArray("posts");
        JSONObject jb= user.getJSONObject(0);
        String event = jb.getString("activities");  
        txtEvent.setText(event);
        }catch(Exception e)
        {
            e.printStackTrace();
        }

    }
}

然后删除java文件末尾的一个}(如果你愿意,也可以重新注册)