2014-02-08 45 views
0

我在一個類中有兩個asynctask,並且在同時執行它們時遇到問題。 這是我的場景,我的用戶點擊查看一月日曆按鈕,它會顯示日曆月份和評論的事件。事件和評論存儲在MySQL數據庫,所以我不得不取得不同的網址。我用asyctask來獲取網址。每當我打電話給其他asynctask它都無法解決。幫幫我!如何同時執行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(); 
       } 

      } 

} 

}

+1

你會得到什麼錯誤? 「無法解決」是什麼意思? – fiddler

+0

確實錯誤stacktrace會有幫助 – injecteer

回答

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> { 
... 
} 
0

您聲明很多事情第一的AsyncTask我強烈reccomend的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(); 
     } 

    } 
} 

後關閉您的AsyncTask內,然後除去沉在Java文件的最後一個}(也重新縮進如果你願意)