2013-05-04 61 views
1

我想查詢使用循環J Web服務和這個操作我想向用戶顯示其進度對話框中執行的線程。操作完成後,我希望進度對話框關閉並啓動新的活動意圖。Android的 - 如何適應的AsyncTask doInBackground方法

我知道的AsyncTask是要走的路。在onPreExecute方法上,我顯示進度對話框。在doInBackground上,我正在執行網絡操作。並onPostExecute我解散對話框並開始一個新的活動意圖。

我的問題是doInBackground將執行循環J networkcall異步所以onPostExecute會先完成我的網絡操作之前​​。如果你看我的日誌,它會顯示:

「啓動新的活動!」 「獲取類別服務!」

「擷取的類別的服務!」 「開始新的活動!」

我如何適應運行doInBackground異步任務? onPostExecute有沒有辦法等到我的異步loopJ操作完成?

public class FetchCategoriesServices extends AsyncTask<HITECategory, String, String> 
{ 
    private Category userSelectedCategory; 
    private ProgressDialog busyDialog; 

    @Override 
    protected void onPreExecute() 
    { 
     busyDialog = ProgressDialog.show(SearchActivity.this, getApplicationContext().getString(R.string.progressDialogTitle), 
             getApplicationContext().getString(R.string.progressDialogMessage)); 
    } 

    @Override 
    protected String doInBackground(HITECategory... params) 
    { 
     userSelectedCategory = params[0]; 

     String requestCategoryServiceURL = BASE_URL + "GetServices?CategoryID=" + userSelectedCategory.categoryID + "&ResponseType=JSON"; 

     try 
     { 
      Client.get(requestCategoryServiceURL, new AsyncHttpResponseHandler() 
      { 
       @Override 
       public void onSuccess(String jsonResponse) 
       { 
        Gson gson = new Gson(); 
        CategoryServicesListResponse Response = gson.fromJson(jsonResponse, CategoryServicesListResponse.class); 

        categoryServiceresults = Response.categoryServices; 

        Log.d(getString(R.string.DebugKey), "Fetched category services!"); 
       } 
      }); 
     } 
     catch (Exception e) 
     { 
      Log.d(getString(R.string.DebugKey), "Error connecting to service and fetching category services list"); 
     } 

     return null; 
    } 

    @Override 
    protected void onPostExecute(String params) 
    { 
     busyDialog.dismiss(); 
     Log.d(getString(R.string.DebugKey), "Starting new activity!"); 
     Intent intent = new Intent(getApplicationContext(), CategoriesSearchActivity.class); 
     startActivity(intent); 
    } 
} 
+0

看起來像你對我並不需要一個'AsyncTask'在一切:循環J的要求已經是異步的,並提供了當的方法工作即將開始並完成。只要確保你做的UI線程上的任何UI相關的調用(即通過它發佈到'Handler'這是在UI線程上創建的)。 – 2013-05-04 00:50:25

回答

2

只要把onPostExecute代碼爲onSuccess方法:

 Client.get(requestCategoryServiceURL, new AsyncHttpResponseHandler() 
     { 
      @Override 
      public void onSuccess(String jsonResponse) 
      { 
       Gson gson = new Gson(); 
       CategoryServicesListResponse Response = gson.fromJson(jsonResponse, CategoryServicesListResponse.class); 

       categoryServiceresults = Response.categoryServices; 

       Log.d(getString(R.string.DebugKey), "Fetched category services!"); 
       youractivity.this.runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
          busyDialog.dismiss(); 
          Log.d(getString(R.string.DebugKey), 
              "Starting new activity!"); 
          Intent intent = new Intent(getApplicationContext(), 
           CategoriesSearchActivity.class); 
          youractivity.this.startActivity(intent); 
        } 
       }); 
      } 
     });   
+0

哈,我完全沒有想到這一點。我忙於考慮onPostExecute。感謝你的回答! – CLDev 2013-05-04 00:47:55

相關問題