2014-09-10 87 views
-1

我想在onPostExecute方法中得到响应。目的是在测试结果值后显示AlertDialog,所以我在doInBackground中完成了它,但不是我想的逻辑?而且我认为这样做在onPostExecute,但仍然不知道我怎么能得到response.Ther是我的代码如何获得onPostExecute中的响应?

//@Override 
    protected void onPostExecute(final Boolean success/*,String result*/) { 
     //LoginTask = null; 
     //showProgress(false); 
     //delegate.processFinish(result); 

     if (success) { 
      /* AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
      builder.setTitle("Succes !"); 
      builder.setMessage("your new password has been sent to the email address you specified. \nThink to change it later!") 
        .setCancelable(false) 
        .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
          dialog.cancel(); 
         } 
        }); 
      AlertDialog alert = builder.create(); 
      alert.show();*/ 
      finish(); 
     } else { 
      //password.setError(getString(R.string.error_incorrect_password)); 
      //password.requestFocus(); 
      /*AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
      builder.setTitle("Error !"); 
      builder.setMessage("This email does not exist") 
        .setCancelable(false) 
        .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
          dialog.cancel(); 
         } 
        }); 
      AlertDialog alert = builder.create(); 
      alert.show();*/ 

     } 

    } 
+0

onPOST等和OnPre是专为做UI操作和doInBackground FO r后端操作 – 2014-09-10 08:31:29

+0

但它不回答我的问题 – 2014-09-10 08:38:04

+0

请发布更多代码。为什么@Override注释掉了? – Intrications 2014-09-10 08:39:28

回答

1

你可以使用异步像这样:

public class asy extends AsyncTask<Void, Void, String>{ 

    @Override 
    protected String doInBackground(Void... params) { 
     // do your work,and return result in string 
     //you can send null if not success else return string 
     if(your work was true) 
      return result; 
     else 
      return null; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     super.onPostExecute(result); 
     //do any thing with result 
     if(result == null){ 
      //not success 
     }else{ 
      //success 
     } 
    } 

} 
+0

我有一个错误:super.onPostExecute(result);结果应该是一个布尔值而不是字符串 – 2014-09-10 08:50:47

+0

是的,你可以使用布尔值,但我看到你也需要字符串,所以你可以使用这两个你的target.if你不需要字符串作为结果,你可以使用一些东西以上答案 – MHP 2014-09-10 10:43:38

0

记得使用布尔(包装类)不是布尔数据类型

public class asyncTask extends AsyncTask<Void, Void, Boolean>{ 

    @Override 
    protected Boolean doInBackground(Void... params) { 
     //your background code 
     if(background code is true) 
      return true; 
     else 
      return false; 
    } 

    @Override 
    protected void onPostExecute(Boolean result) { 
     super.onPostExecute(result); 
     if(result == true){ 
      //success 
     }else if(result == false{ 
      //not success 
     } 
    } 

}