Android Async Task未达到onPostExecute

时间:2013-04-08 17:09:32

标签: android asynchronous

我有一个使用异步任务验证登录信息的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();
    }
}

1 个答案:

答案 0 :(得分:2)

你有两个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();
    }
}
相关问题