gpt4 book ai didi

java - onPostExecute 不会被触发

转载 作者:行者123 更新时间:2023-11-30 02:35:02 24 4
gpt4 key购买 nike

所以我创建了一个 web 服务调用类,它调用我在 AsyncTask 上扩展的 web 服务:

public class WebServiceLoginCall extends AsyncTask{

@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}

protected void onPostExecute(Void result) {
try {
if(loginStatus){
System.out.println("onpost has been executed");
//Navigate to Home Screen
loginIntent = new Intent(getBaseContext(), HomeActivity.class);
startActivity(loginIntent);
//prevents user to go back to the login screen
finish();
}else{
//Set Error message
Toast.makeText(getApplicationContext(), "Login Failed. Check your details.", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "An error occured. Check your mobile connection.", Toast.LENGTH_SHORT).show();
}
}

@Override
protected Boolean doInBackground(Object... arg0) {
System.out.println("doinbackground triggered");
try {
loginStatus = Webservice.invokeLoginWS(loginUser.get_userEmail(), loginUser.get_userPassword());
} catch (Exception e) {
System.out.println("an error occured with the webservice");
}

return loginStatus;
}

}

当用户按下登录按钮时,我使用以下代码:

public void onClick(View v) {
switch (v.getId()) {
case R.id.btnLogin:
email = (EditText) findViewById(R.id.txtEmail);
loginUser = new User();
loginUser.set_userEmail(email.getText().toString());
loginUser.set_userPassword(password.getText().toString());

//starts loging webservice
WebServiceLoginCall task = new WebServiceLoginCall();
//executes the login task
task.execute();

break;

当我检查时,在我的控制台中触发了 doInBackground,但没有触发 onPostExecute。我做错了什么吗? doInBackground 不会抛出任何异常。

最佳答案

进行以下两处修改

1.使用public class WebServiceLoginCall extends AsyncTask<Void, Void, Boolean > 而不是 public class WebServiceLoginCall extends AsyncTask

2.使用

@Override
protected void onPostExecute(Void result) {
// your code
}

不仅仅是

protected void onPostExecute(Void result) {
// your code
}

Refer Android- Async task

解释:

在您的情况下,如果您在没有扩展通用 Asynctask 的情况下将 @Override 放在 onPostExecute() 上,则会出现编译时错误。因此,您必须进行以上两项更改。

希望对你有帮助。

关于java - onPostExecute 不会被触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26712587/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com