2012-04-30 82 views
4

我正在編寫一個主要使用從Web服務獲取的數據的應用程序,並且我想使用AsyncTask在後臺運行SOAP調用...我對Android相當陌生(正在在iOS程序員),所以我在這個有點新...Android,帶有kSoap2的AsyncTask

現在,我有一個登錄界面,在這裏我把用戶提供的登錄和檢查對信息的服務器上...

在我登錄活動

所以:

loginBtn.setOnClickListener(new OnClickListener() 
    { 
     public void onClick(View v) 
     { 
      //Run the connection to authenticate the user 
      AuthenticateConnection mAuth = new AuthenticateConnection(); 

      mAuth.mNumber = number; 
      mAuth.mPassword = pass; 

      mAuth.connection(); 
     } 
    } 

和我的皁類是這樣的:

public class AuthenticateConnection 
{ 
    private static final String SOAP_ACTION = "http://tempuri.org/Authenticate"; 
    private static final String METHOD_NAME = "Authenticate"; 
    private static final String NAMESPACE = "http://tempuri.org/"; 
    private String URL; 

    public Boolean userOK; 

    public String mNumber; 
    public String mPassword; 

    public AuthenticateConnection() 
    { 

    } 

    public void connection() 
    { 
     Singleton service = Singleton.getInstance(); 
     String firstURL = service.getURL(); 
     URL = firstURL + "Parent.svc"; 

     System.out.println("Connection to: " + URL); 

     //Initialize soap request 
     SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 

     //Add parameters 
     request.addProperty("login", mNumber); 
     request.addProperty("password", mPassword); 

     //Declare the version of the SOAP request 
     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 

     envelope.dotNet=true; 
     envelope.implicitTypes=true; 
     envelope.setAddAdornments(false); 

     //Prepare request 
     envelope.setOutputSoapObject(request); 

     //Needed to make the internet call 
     HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 

     //Allow for debugging - needed to output the request 
     androidHttpTransport.debug = true; 

     try 
     { 
      //this is the actual part that will call the web service 
      androidHttpTransport.call(SOAP_ACTION, envelope); 

      //Get the SoapResult from the envelope body. 
      //Object result = envelope.getResponse(); 
      //Object result = envelope.bodyIn; 
      SoapObject sResult = (SoapObject)envelope.bodyIn; 

      String tempID = sResult.getProperty("AuthenticateResult").toString(); 

      //Check if the user exists and has the correct password 
      if(tempID != "-1") 
      { 
       userOK = true; 

       //Store the values in the singleton class 
       service.parentID = sResult.getProperty("AuthenticateResult").toString(); 
       service.parentToken = sResult.getProperty("token").toString(); 
      } 

      //If -1 is returned, then either the number or the password is incorrect 
      else 
      { 
       userOK = false; 
      }   
     } catch(org.xmlpull.v1.XmlPullParserException ex2) 
     {    
      //System.out.println(androidHttpTransport.requestDump.toString()); 

     } catch (Exception e) 
     { 
      e.printStackTrace(); 
      System.out.println(androidHttpTransport.requestDump.toString()); 
     } 
    } 
} 

所以我的問題是,我會如何做到這一點與AsyncTask? 我一直在尋找上的AsyncTask一些教程,但還沒有真正「得到它」那麼遠,

回答

6

你可以這樣做:

private class ConnectionTask extends AsyncTask<String, Void, Void> { 
    private ProgressDialog dialog = new ProgressDialog(ACTIVITY_NAME.this); 

    protected void onPreExecute() { 
     dialog.setMessage("Connecting..."); 
     dialog.show(); 
    } 

    protected void doInBackground(String... args) { 
     AuthenticateConnection mAuth = new AuthenticateConnection(); 
     mAuth.mNumber = args[0]; 
     mAuth.mPassword = args[1]; 
     mAuth.connection(); 
    } 

    protected void onPostExecute(Void v) { 
     if (dialog.isShowing()) { 
      dialog.dismiss(); 
     } 
    } 
} 

然後調用它:

loginBtn.setOnClickListener(new OnClickListener() 
{ 
    public void onClick(View v) 
    { 
     //Run the connection to authenticate the user 
     new ConnectionTask().execute(number, pass); 
    } 
} 

您的connection方法AuthenticateConnection應返回一些內容以確保用戶已通過身份驗證。然後你可以使用這樣的onPostExecute該值,事:

protected void onPostExecute(Integer res) { 
     if (dialog.isShowing()) { 
      dialog.dismiss(); 
     } 
     if (res.intValue() == OK) { 
      /* Maybe start a new Activity ...*/ 
     } else { 
      /* Maybe show a Toast with an error message ...*/ 
     } 
    } 

在這種情況下的AsyncTask的簽名將發生變化: private class ConnectionTask extends AsyncTask<String, Void, Integer> 和doInBackground應返回一個Integer

希望它有幫助。