0

I have two asynctask in one class and i am having trouble in executing them at the same time. Here is my scenario, my the user clicks the view january calendar button it will show up the events for the month of calendar and the comments for it. The events and comments are stored in mysql database so i have to fetch different url. I used asyctask to fetch the url. Whenever i call the other asynctask it cannot be resolved. help me out!

Here is my code:

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();
                }

            }

}

}

IamMe.
  • 51
  • 3
  • 13

2 Answers2

1

Your second asynctask public class LoadCommentCal extends AsyncTask is inside the first asynctask! You need to move it out of the first asynctask.

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

... other functions

private class LoadCommentCal extends AsyncTask<Void, Void, String> {
...
}
StarsSky
  • 6,721
  • 6
  • 38
  • 63
0

You are declaring lots of things inside the first AsyncTask I strongly reccomend to close your AsyncTask after the onPostExecute

    @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();
        }

    }
}

and then removing one } at the end of the java file (and also reindent if you wish)

kalup
  • 61
  • 1
  • 6