public class checkSchoolCodeQuery extends AsyncTask<String, Integer, String> {
EditText etSchoolCode = (EditText) findViewById(R.id.etSchoolCode);
String schoolcode = etSchoolCode.getText().toString();
String isValid = "didn run";
@Override
protected String doInBackground(String... params) {
final ParseQuery<ParseObject> query = ParseQuery.getQuery("Schools");
query.whereEqualTo("schoolcode", schoolcode);
query.countInBackground(new CountCallback() {
@Override
public void done(int i, ParseException e) {
if (e == null) {
if (i == 1) {
// Check if expiry date is greater than today's date
query.getFirstInBackground(new GetCallback<ParseObject>() {
@Override
public void done(ParseObject parseObject, ParseException e) {
if (e == null) {
Date expiryDate = parseObject.getDate("expirydate");
Date todaysDate = new Date();
System.out.println(expiryDate);
if (expiryDate.after(todaysDate)) {
isValid = "true";
} else {
isValid = "false";
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getApplicationContext())
.setMessage("Your school doesnt have access");
}
} else {
System.out.println("There was an error when checking if School Code was valid");
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getApplicationContext())
.setMessage("There was an error when checking if School Code was valid");
AlertDialog alertDialog = alertDialogBuilder.show();
isValid = "didnt run";
}
}
});
} else {
isValid = "false";
// Create Alert Dialog to show error text
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getApplicationContext())
.setMessage("The School Code is invalid");
AlertDialog alertDialog = alertDialogBuilder.show();
}
} else {
isValid = "didnt run";
System.out.println("There was an error when checking if School Code was valid");
// Create Alert Dialog to show error text
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getApplicationContext())
.setMessage("There was an error when checking if School Code was valid");
AlertDialog alertDialog = alertDialogBuilder.show();
}
}
});
return isValid;
}
@Override
protected void onPostExecute(String aString) {
if (aString == "true") {
System.out.println("The school code is valid");
} else if (aString == "false") {
System.out.println("The school coe is invalid");
}
else {
System.out.println("There was an error");
}
}
}
当我运行我的代码时,isValid
变量不会更新,因此它使用isValid
字符串的初始值。我错误地实施了AsyncTask
还是错过了什么?如何在AsyncTask
中更改变量?请帮忙,因为我是编程的初学者。我已经检查了其他问题,但它们不适用于我的情况;它们都是高级场景,我不理解他们的代码。我只是想找一个简单的解决方法。
答案 0 :(得分:0)
通过尝试使用太多层的后台线程,你已经使这个过于复杂。
AsyncTask
在后台线程中执行doInBackground
方法。在其中,您在另一个后台线程中启动查询以更新isValid
变量,然后您立即返回isValid
的当前(未更改)值作为{{1}的结果},在查询运行之前。在调用查询回调时,由于AsyncTask
已经执行,因此不再查看isValid
的值。
解决方案是,由于您已经在return isValid;
内的后台线程上,因此请不要使用在其他后台线程上执行的查询对象的方法。在AsyncTask.doInBackground
内,直接执行查询。这也可以让您将AsyncTask.doInBackground
的范围缩小到局部变量而不是字段。或者,由于ParseQuery
的各种isValid
方法显然已经专门设计用于Android UI线程,因此您可以使用其中一种没有inBackground
的方法。
我从未做过Android编程,所以我不熟悉这些课程中的任何一个,所以我可能还有另一种可能性。
目前还没有导致代码出现问题的另一个问题,但很容易因为它太脆弱而试图使用AsyncTask
存储程序状态信息。将它与String
而不是equals
进行比较也很狡猾,尽管目前这对于字符串文字实际上是无害的。当你试图诊断另一个问题时,可能只是测试代码,但如果不是,那么用枚举或可以为空的==
来实现这样的三态更好。或者别的什么:
Boolean