-2

我有一个使用异步任务来验证登录信息的 android 应用程序。onpostexecute 永远不会到达,我无法将@Override 添加到它以使其运行。当我尝试@Override 时,eclipse 说它必须覆盖超类型的方法。这是代码。

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();
    }
}
4

1 回答 1

2

你有两个onPostExecute()s 删除空的,并确保做某事的那个在 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();
    }
}
于 2013-04-08T17:10:42.793 回答