2013-04-08 177 views
-2

我有一個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(); 
    } 
} 
+0

你的問題很可能與兩個'onPostExecute()'方法有關。一探究竟。 – Sajmon 2013-04-08 17:31:45

回答

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

對不起,我在eclipse中通過自動修復添加了額外的東西。原來,我在課堂外有onPostExecute,但上面的答案對於發佈的代碼是正確的。 – ed209 2013-04-10 14:58:26