2013-11-09 33 views
0

我有多次執行Asynctask的要求。因此,我每次都創建一個新的Asynctask實例。僞代碼如下創建新實例時,AsyncTask沒有執行兩次

public void sendToServer(final String messagetoSend,Context context){ 

AsyncHttpPost wst = new AsyncHttpPost(messagetoSend); 
wst.execute(); 
System.out.println("GET OUT"); 
System.out.println("SENDING TO SERVER"); 
} 

我從onResume方法調用sendToServer功能。第一次執行Asynctask時不會失敗。但第二次,它沒有得到執行。我在logcat中獲取了GETOUT和SENDING TO SERVER語句。但是這個任務並沒有得到執行。讓我知道我失蹤的地方。謝謝你的幫助。

+1

試試這個:wst.execute(); 後api級別更大11:\t \t \t \t wst.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,null);異步任務池是低於11的一個。所以它正在等待執行它。 –

+0

謝謝Ramesh。這是行得通的。我也針對android版本少於11.在這種情況下該怎麼辦? –

+0

讓它成爲,有一件事使得你的AsyncHtpppost是globel和cacel它,如果已經存在。 –

回答

0

你只是在這裏調用一次。要再次調用它,你將不得不包括旁邊的前一個像下面的另一個wst.execute()聲明: -

public void sendToServer(final String messagetoSend,Context context){ 

    AsyncHttpPost wst = new AsyncHttpPost(messagetoSend); 
    wst.execute(); 
    wst.execute(); 
    System.out.println("GET OUT"); 
    System.out.println("SENDING TO SERVER"); 
} 
0

只要寫新AsyncHttpPost(messagetoSend).execute(),而其它3行。如果你想要定位不同的版本,只需製作一個if語句來檢查設備的版本,然後使用其中的一個或另一個實現。