簡體   English   中英

Android:在 Toast 中對異步任務 onpostexecute() 的響應相同

[英]Android: Same response in Toast for async task onpostexecute()

我正在使用 web 服務,我在 onpostexecute() 方法中的 Toast 出現問題。 我正在嘗試根據 logcat response.resultsRequestSOAP 顯示 toast 是我從服務器檢索的字符串值。 如果字符串值為 1,那么我應該在 toast 中顯示“已注冊”。 如果 0 則“重試”,如果 -1 則“字段為空”

在我的 logcat 中,我將響應作為字符串值。

問題是每次我收到“再試一次”吐司消息時。

問題可能出在布爾值(我正在使用)而不是從不返回 null 的原始值上。 如果(resultsRequestSOAP.booleanValue()),我對這一行感到困惑。 它不適合我。 如何解決這個問題?

請有人檢查 onpostexecute() 中的代碼並讓我知道問題所在。

我想比較logcat中的值並像這樣顯示吐司。 如果為 1 則“已注冊”,如果為 0 則“重試”,如果為 -1 則“字段不應為空”

總是感謝您的幫助...!

    public class Register extends Activity {

       public static final  Boolean resultsRequestSOAP = null;

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

 private class RegisterTask extends AsyncTask<Void, Void, Boolean> {
      private final ProgressDialog dialog = new ProgressDialog(Register.this);

      protected void onPreExecute() {
         this.dialog.setMessage("Registering...");
         this.dialog.show();


 public Boolean register() {

  // code for webservices********  
     }

 return resultsRequestSOAP;

protected void onPostExecute( Boolean resultsRequestSOAP) {
         if (this.dialog.isShowing()) {
            this.dialog.dismiss();
         }
         if (resultsRequestSOAP == null) {
             Toast.makeText(Register.this.getApplicationContext(), "Try Again", Toast.LENGTH_SHORT).show();
         }

         else if (resultsRequestSOAP.booleanValue()) {
         //also show register success dialog
             Toast.makeText(Register.this.getApplicationContext(), "Registerd", Toast.LENGTH_SHORT).show();


         }
         else{
             Toast.makeText(Register.this.getBaseContext(), "Field is empty", Toast.LENGTH_SHORT).show();
         }
         super.onPostExecute(resultsRequestSOAP);
      }
        protected void onPostExecute( Boolean resultsRequestSOAP) {
                 if (this.dialog.isShowing()) {
                    this.dialog.dismiss();
                 }
                 sendResult(resultsRequestSoap);
super.onPostExecute(resultsRequestSOAP);


    }

將此方法放在外部 class

public void sendResult(String resultsRequestSoap){
if (resultsRequestSOAP == null) {
                 Toast.makeText(this, "Try Again", Toast.LENGTH_SHORT).show();
             }

             else if (resultsRequestSOAP.booleanValue()) {
             //also show register success dialog
                 Toast.makeText(this, "Registerd", Toast.LENGTH_SHORT).show();


             }
             else{
                 Toast.makeText(this, "Field is empty", Toast.LENGTH_SHORT).show();
             }


}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM