2017-06-09 41 views
0

我需要從「layer_1」中獲取使用SOAP請求的Async方法的信息,然後才從「layer_2」接收信息,但有時候會發生相反的情況,有沒有辦法讓「layer_2」等待「layer_1」的結束?Android Asyntask,第二個參數是發送信息之前的第一個

它我的AsyncTask:

protected Void doInBackground(String... params) { 

     String ope = params[0]; 
     switch (ope) 
     { 
      case "layer_1": 
       if(Util.isNetworkAvailable(mContext.getApplicationContext())) 
       { 
        getWebservice = WebService.getLayer(COD_MATRICULA); 
        changeAsync = true; 
       } 
       break; 
      case "layer_2": 
       if (Util.isNetworkAvailable(mContext.getApplicationContext())) { 
        if(canMade) 
         message = addUserWeb(User); 
        changeAsync = true; 
        callMessage = true; 
       } 
       else 
       { 
        message = null; 
        changeAsync = false; 
       } 
       break; 
     } 
     publishProgress(); 
     return null; 
    } 

,並在我的progressupdate:

protected void onProgressUpdate(Void... values) { 

     if (changeAsync) 
      verifyUser(); 

     if(callMessage) 
     { 
      ....... 
      callMessage = false; 
     } 

    } 

回答

0

您可以使用兩種的AsyncTask在您的項目像下面

//layer_1 
private class AsyncTaskRunner1 extends AsyncTask<String, String, String> { 

    @Override 
    protected void onPreExecute() { 
    } 

    @Override 
    protected String doInBackground(String... params) { 
     if(Util.isNetworkAvailable(mContext.getApplicationContext())) 
      { 
       getWebservice = WebService.getLayer(COD_MATRICULA); 
       changeAsync = true; 
      } 

    } 

    @Override 
    protected void onPostExecute(String result) { 
     AsyncTaskRunner2 myTask = new AsyncTaskRunner2(); 
     myTask.execute(); 
    } 
} 

//layer_2 
private class AsyncTaskRunner2 extends AsyncTask<String, String, String> { 

    @Override 
    protected void onPreExecute() { 
    } 

    @Override 
    protected String doInBackground(String... params) { 
     if (Util.isNetworkAvailable(mContext.getApplicationContext())) { 
       if(canMade) 
        message = addUserWeb(User); 
       changeAsync = true; 
       callMessage = true; 
      } 

    } 

    @Override 
    protected void onPostExecute(String result) { 

    } 
} 
+0

我想過這個問題,但可行嗎? –

+0

它是我的項目工作 –

+0

你是否嘗試這個代碼 –

相關問題