2013-05-10 24 views
1

我是Android開發的新手。我正在嘗試開發一個應用程序,它將與.net webservice連接以便檢索數據。我想用異步任務進行ksoap2調用。我如何用asynctask將它稱爲asyncronus?如何在異步任務中創建ksoap2調用?

我的SOAPCall類是

public class SoapCall { 

public final static String SOAP_ACTION = "http://www.alpha.net.com/ExecuteEBSCommand"; 

public final static String OPERATION_NAME = "ExecuteEBSCommand"; 

public final static String NAMESPACE = "http://www.alpha.net.com"; 

public final static String URL = "http://192.168.2.100/Ebs2Alpha/Service.asmx"; 





public String connection(String Command, String CommandParameters) throws Throwable, Throwable { 
    String response = null; 
    SoapObject Request = new SoapObject(NAMESPACE, OPERATION_NAME); 
    Request.addProperty("strCommand", Command); 
    Request.addProperty("strCommandParameters", CommandParameters); 



    SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
      SoapEnvelope.VER11); 
    soapEnvelope.dotNet = true; 
    soapEnvelope.setOutputSoapObject(Request); 
    // Needed to make the internet call 

    // Allow for debugging - needed to output the request 

     HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 
     androidHttpTransport.debug = true; 
     // this is the actual part that will call the webservice 
     androidHttpTransport.call(SOAP_ACTION, soapEnvelope); 

     // Get the SoapResult from the envelope body. 
     SoapObject result = (SoapObject) soapEnvelope.bodyIn; 

     response = result.getProperty(0).toString(); 


    return response; 
    } 

}

到目前爲止,我得到通過調用主要活動的連接方法與

SoapCall call1= new SoapCall(); 

call1.connection("get_clients", "%"); 

回答

2

使用asynctask很簡單。這是一個例子。

public class MyTask extends AsyncTask<String, Integer, String>{ 


    @Override 
    protected String doInBackground(String... params) { 
    String response = null; 
    SoapObject Request = new SoapObject(NAMESPACE, OPERATION_NAME); 
    Request.addProperty("strCommand", params[0]); 
    Request.addProperty("strCommandParameters", params[1]); 



    SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
     SoapEnvelope.VER11); 
    soapEnvelope.dotNet = true; 
    soapEnvelope.setOutputSoapObject(Request); 
    // Needed to make the internet call 

    // Allow for debugging - needed to output the request 

    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 
    androidHttpTransport.debug = true; 
    // this is the actual part that will call the webservice 
    androidHttpTransport.call(SOAP_ACTION, soapEnvelope); 

    // Get the SoapResult from the envelope body. 
    SoapObject result = (SoapObject) soapEnvelope.bodyIn; 

    response = result.getProperty(0).toString(); 


    return response; 
} 

}

和呼叫與參數的任務。

MyTask myTask = new MyTask(); 
myTask.execute(new String[] {Command, CommandParameters}); 

希望它會有所幫助。

0

響應我建議你使用AsyncTaskLoader對我來說比AsyncTask更容易。 看一看here,這個例子非常廣泛,看起來很嚇人,你可能會發現更簡單的。這個想法是,你的Activity實現LoaderCallbacks用於創建加載器和一個在加載器完成時被調用的方法。您通過LoaderManager「開始」一個裝載機。 AsynctaskLoader是一個類extends AsyncTaskLoader並做異步的東西。

我給你一個簡單的例子:

這是AsyncTaskLoader:

public class StartupLoader extends AsyncTaskLoader<Boolean> { 

Context context; 

public StartupLoader(Context context) { 
    super(context); 
    this.context = context; 
    forceLoad(); 

} 

@Override 
public Boolean loadInBackground() { 

    // DO STUFF! 

    return true; 
} 

@Override 
protected void onStopLoading() { 

} 

@Override 
public void onCanceled(Boolean data) { 
    super.onCanceled(data); 

} 

@Override 
protected void onReset() { 
    super.onReset(); 



} 

} 

這是你在將啓動加載程序的活動,這是一個內部類:

public class StartupCallback implements 
     LoaderManager.LoaderCallbacks<Boolean> { 
    @Override 
    public void onLoadFinished(Loader<Boolean> loader, Boolean succ) { 

     // Here you get your results back 

    } 

    @Override 
    public Loader<Boolean> onCreateLoader(int id, Bundle args) { 

     return new StartupLoader(getApplicationContext()); 
    } 

    @Override 
    public void onLoaderReset(Loader<Boolean> loader) { 

    } 
} 

這就是你如何開始從徘徊無論你想要的裝載機(即活動內):

StartupCallback startupCallback = new StartupCallback(); 
getSupportLoaderManager().initLoader(0, null, startupCallback); 

其中0是您給加載器的ID,null是一個參數的包。 祝你好運:)