所以我创建了一个webservice调用类,调用我的webservice,它扩展了AsyncTask:
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不会抛出任何异常。
答案 0 :(得分:1)
进行以下两项更改
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
}
说明:
在你的情况下如果你在onPostExecute()上放置@Override而不扩展通用的Asynctask,你将得到一个编译时错误。因此,您必须进行以上两项更改。
希望它可以帮到你。答案 1 :(得分:0)
因为你正在调用一个不同的方法,你的帖子执行将不会执行,如果你像你的代码一样定义
使用
@Override
protected void onPostExecute(boolean result) {
}
答案 2 :(得分:0)
因为你没有覆盖onPostExecute()方法
添加:@Override
@Override
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();
}
}