我遇到一个问题,即如果需要结果,我无法检查AsyncTask结果的值以启动新活动。
这是我的onPostExecute方法:
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Toast.makeText(LoginActivity.this, s, Toast.LENGTH_SHORT).show();
}
它从php文件中提取字符串值,它有两个可能的值,“您已成功登录”或“电子邮件或密码不正确”。一切正常,直到我想检查烘烤的消息是什么,因为如果登录成功,我需要开始一个新的活动。
这就是我尝试这样做的方式:
public void onClick(View v) {
AttemptLogin login = new AttemptLogin();
try {
String res = login.execute("http://10.0.2.2/loginApp.php").get();
if (res.equals("You are successfully logged in"))
startActivity(new Intent(LoginActivity.this,ConnectActivity.class));
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
我很困惑,因为当我敬酒res时,我得到了理想的信息,所以问题出在这一行, if(res.equals(“你已成功登录”))。该应用程序的行为类似于此行根本不存在。
我也试过if(AsyncTask.getStatus()== FINISHED),然后检查AsyncTask结果但是没有帮助。
我真的不知道发生了什么,有人可以帮我解决这个问题吗?
答案 0 :(得分:1)
AsyncTask具有OnPostExecute和OnPreExecute方法。 它们都可以调用项目,变量和方法,就像它们是正常的方法一样。
因此,在您的onPostExecute中,您可以使用以下内容轻松检查结果并开始您的活动:
@Override
protected void onPostExecute(String s) {
super.onPostExecute();
try {
if (s.equals("You are successfully logged in")){
Intent i = new Intent(LoginActivity.this,ConnectActivity.class);
startActivity(i);
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
Toast.makeText(LoginActivity.this, s, Toast.LENGTH_SHORT).show();
}