Android异步任务未到达onPostExecute
问题描述:
我有一个Android应用程序正在使用异步任务来验证登录信息。 onpostexecute永远不会到达,我不能将@Override添加到它以使其运行。当我尝试@Override时,eclipse说它必须重载一个超类型的方法。这是代码。Android异步任务未到达onPostExecute
public class UserLoginTask extends AsyncTask <LoginObject, Void, Boolean>
{
@Override
protected void onPostExecute(Boolean result)
{
// TODO Auto-generated method stub
super.onPostExecute(result);
}
@SuppressWarnings("finally")
@Override
protected Boolean doInBackground(LoginObject... params)
{
// TODO: attempt authentication against a network service.
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://softeng.cs.uwosh.edu/students/cs342g6/login.php");
Boolean response = false;
try
{
//Convert the login object to XML
XStream xstream = new XStream(new DomDriver("UTF-8"));
xstream.alias("Login", LoginObject.class);
String xml = xstream.toXML(login);
// Pass the XML as a StringEntity
StringEntity se = new StringEntity(xml,HTTP.UTF_8);
se.setContentType("text/xml");
httppost.setEntity(se);
System.out.println("MADE IT TO RESPONSE");
HttpResponse httpresponse = httpclient.execute(httppost);
HttpEntity resEntity = httpresponse.getEntity();
String resp = EntityUtils.toString(resEntity);
System.out.println(resp);
response = convertToBool(resp);
if(response)
System.out.println("true");
else
System.out.println("false");
}
catch (ClientProtocolException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return response;
}
}
protected void onPostExecute(Boolean success)
{
System.out.println("In onPostExecute");
mAuthTask = null;
showProgress(false);
if (success)
{
finish();
}
else
{
mPasswordView
.setError(getString(R.string.error_incorrect_password));
mPasswordView.requestFocus();
}
}
答
你有两个onPostExecute()
动手清除一空,并确保做一些事情之一是的AsyncTask类中。
@Override
protected void onPostExecute(Boolean result)
{
System.out.println("In onPostExecute");
mAuthTask = null;
showProgress(false);
if (success)
{
finish();
}
else
{
mPasswordView
.setError(getString(R.string.error_incorrect_password));
mPasswordView.requestFocus();
}
}
+0
对不起,我在eclipse中通过自动修复添加了额外的东西。原来,我在课堂外有onPostExecute,但上面的答案对于发布的代码是正确的。 – ed209 2013-04-10 14:58:26
你的问题很可能与两个'onPostExecute()'方法有关。一探究竟。 – Sajmon 2013-04-08 17:31:45