2016-09-26 28 views
0

在我的應用程序中,我使用調用AsyncTask-A來檢查用戶是否是新用戶,並致電AsyncTask-B提示用戶輸入新密碼並重置它。我開始知道顯示對話框應該在onPreExecute()上完成,然後在doInBackground()上更改密碼(使用KSOAP)。但是,問題在於alert.show();不會等待,並且會立即調用doInBackground()中斷邏輯。這裏是代碼的相關部分:AsyncTask onPreExcute()中的對話框

private class AsyncTaskPasswordResetter extends AsyncTask<String, Void, Void> 
{ 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 

     AlertDialog.Builder builder = new AlertDialog.Builder(login.this); 
     builder.setMessage("Please enter new password"); 
     final EditText input = new EditText(login.this); 
     input.setText(""); 
     builder.setView(input); 

     builder.setPositiveButton("OK", new DialogInterface.OnClickListener() 
       { 
        public void onClick(DialogInterface dialog, int which) 
        { 
         SoapObject request = new SoapObject("http://tempuri.org/", "ChangeParentPassword"); 
         request.addProperty(MyUtils.CreateProp("Username", userName, String.class)); 
         request.addProperty(MyUtils.CreateProp("Password", password, String.class)); 
         SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11) {{dotNet = true;}}; 
         envelope.setOutputSoapObject(request); 
         HttpTransportSE androidHttpTransport = new HttpTransportSE("http://hannuveda.online/default.asmx"); 

         try { 
          androidHttpTransport.call("http://tempuri.org/ChangeParentPassword", envelope); 
          SoapPrimitive response = (SoapPrimitive) envelope.getResponse(); 

          if (response.toString().equals("OK")) { 
           setTextViewstatus("passed\n"); 
          } else { 
           setTextViewstatus("failed\n"); 
           isError = true; 
          } 
         } catch (Exception e) { 
          setTextViewstatus("error\n"); 
          isError = true; 
         } 
        } 
       } 
     ); 
     AlertDialog alert = builder.create(); 
     alert.show(); 
     input.requestFocus(); 
     InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
     imm.hideSoftInputFromWindow(input.getWindowToken(), 0); 
    } 

    @Override 
    protected Void doInBackground(String... params) 
    { 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void aVoid) { 
     super.onPostExecute(aVoid); 
     if(isError) 
      SetLoginButtonEnability(true); 
     setTextViewstatus("wait..."); 
      new Handler().postDelayed(new Runnable() { 
       @Override 
       public void run() { 
        Intent intent = new Intent(login.this, trackbus.class); 
        intent.putExtra("UserID", userID); 
        startActivity(intent); 
       } 
      }, 1000); 
    } 
} 
+2

爲什麼你的AsyncTask中有UI邏輯?你如何做外面的UI邏輯並使用回調。 –

+0

我調用AsyncTaskA來檢查用戶是否是新的,如果用戶是新的,我從它的onPostExecute()調用AsyncTaskB。在AsyncTaskB中,我需要提示用戶(使用對話框)獲取新密碼。任何提示如何使用你所說的邏輯來實現這一點? –

回答

0

爲了有超過連續AsyncTask是一個更好的控制是使用回調是一個好主意。

例如

public class LoginTask extends AsyncTask < String, Void, HttpResponse > { 

private final LoginTaskCallback callback; 

public LoginTask(LoginTaskCallback callback) { 
    this.callback = callback; 
} 


@Override 
protected HttpResponse doInBackground(String...params) { 
    // do network stuff 
} 

@Override 
protected void onPostExecute(HttpResponse response) { 
    // just and example 
    if (response.getStatus() == 200) 
     callback.success(); 
    else 
     callback.failure(); 
} 

public interface LoginTaskCallback { 
    void failure(); 
    void success(); 
} 
} 

然後你可以execute任務和接收回調的做任何在這種情況下,下次啓動AsyncTaskB

new LoginTask(new LoginTask.LoginTaskCallback() { 
      @Override 
      public void failure() { 
       // stop here smth went wrong 
      } 

      @Override 
      public void success() { 
       // start next task 
      } 
     }).execute(); 
+0

何處顯示對話框? –

+0

@AkshayJ在你的'Activity'或者你可以調用AsyncTask的地方。您只需要將'username'和'password'傳遞給任務並執行它。 –

+0

問題是,我需要提示用戶輸入新密碼並在點擊確定後更改密碼。但Show()不阻塞。你能幫助我嗎? –

0

1)從AsyncTask中刪除UI代碼。讓那些在Activity/Fragments
2)點擊按鈕,調用AsyncTask這會照顧網絡操作,並取回響應給調用者

下面的代碼應該不會在的AsyncTask

 AlertDialog.Builder builder = new AlertDialog.Builder(login.this); 
     builder.setMessage("Please enter new password"); 
     final EditText input = new EditText(login.this); 
     input.setText(""); 
     builder.setView(input); 

     builder.setPositiveButton("OK", new DialogInterface.OnClickListener() 
       { 
        public void onClick(DialogInterface dialog, int which) 
        { 
         //This invocation is explained in details given below 
         new NetworkAsyncTask(ActivityClass.this).execute(); 
        } 
       } 
     ); 
     AlertDialog alert = builder.create(); 
     alert.show(); 
     input.requestFocus(); 
     InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
     imm.hideSoftInputFromWindow(input.getWindowToken(), 0); 

創建單獨的AsyncTask,它將採用參數,以便可以使用相同的參數處理其他請求

建議使用方法創建接口,這將返回處理的數據給調用者

public interface OnAsyncTaskComplition { 
    public void networkResponse(Object responseObject); 

} 

實現從去哪兒網的請求被髮送

class DemoActivity extends Activity implements OnAsyncTaskComplition{ 

    @Override 
    public void networkResponse(Object responseObject) { 
    // call back will come here for failure or success 

    } 

} 

繼UI類這個接口的中的AsyncTask爲此,

class NetworkAsyncTask extends AsyncTask<Void,Boolean,Boolean>{ 
     Activity activity; 
     Object responce; 
     ProgressDialog dialog; 
     public NetworkAsyncTask (Activity activity) { 
     super(); 
     this.activity = activity; 
     } 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      //Show a waiting screen 
      dialog = ProgressDialog.show(activity, "Please wait", "Processing........."); 

     } 

     @Override 
     protected Boolean doInBackground(Void... params) { 
     // Do all your processing here 
     //assign the response to OBJECT responce 
     this.responce = processedResult; 
     //return true of false based on the success 
     return true;//or false based on the success 
     } 
     @Override 
     protected void onPostExecute(Boolean aBoolean) { 
      super.onPostExecute(aBoolean); 
      //dismiss the dialog shown for processing 
      if(dialog!=null && dialog.isShowing()) 
       {dialog.dismiss();} 
      activity.networkResponse(responce); 
     } 

} 
+1

@MuratK。沒有人是以編碼者的身份出生的!每個人都從錯誤和考驗中學習。 Akshay J完全沒有來自Android的背景。從他的問題中明確表達出來。如果問題與代碼相關/有足夠的代碼,而不僅僅是想法和鏈接,那麼我們作爲SO應該會有所幫助。所以我想這個幫助是相關的。我沒有干涉你的答案。我期待你的一樣。 – Stallion

0

在AsyncTaskA檢查,如果是新用戶,如果是這樣,那麼不用運行新的AsyncTask,只需運行一個AlertDialog,通過它從用戶那裏獲取數據。
onClickListener將用戶數據作爲參數列表傳遞給AsycnTaskB