2014-06-05 186 views
-1

我是新來的android,我已經構建了一個應用程序,其中使用了很多web服務。我使用異步任務doInBackground()方法調用webservices。android中的異步任務

我的asyntask類是公共的,它被寫入一個單獨的文件。 我不使用onPostExecute執行postwebservice操作, 而是我創建一個字符串並返回。 然後在我的活動類中,我正在檢查字符串並使用java bean打包數據。

因爲Async任務以異步方式運行,所以依賴於doInBackground方法返回的內容是正確的。還是必須僅在onPostExecute中執行我的發佈web服務操作。

請指導我以正確的方式爲我的項目做事。謝謝。

+0

你必須使用onPostExecute。 –

+0

並執行!千萬不要直接調用doInBackground .... – ElDuderino

+0

iam使用execute我沒有直接調用doInBackground,在調用execute之後,我得到了活動 – user3136798

回答

0

您應該在onPostExecute之前完成所有發佈的webservice工作,因爲此方法在主線程(UI)中執行,因此您可以更新UI。

看看http://developer.android.com/reference/android/os/AsyncTask.html

一個基本的例子(從文檔兩者)

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> { 
protected Long doInBackground(URL... urls) { 
    // Here you call your webservice and return the result 
    return totalSize; 
} 

protected void onPostExecute(Long result) { 
    // The result from doinbackground is passed as an argument 
    // here, so you update your UI from this method. 
} 
} 

由於kaushik你也可以用一個定義的回調重用Asyntask,檢查這個問題: