I am a newbie on android development. I am trying to develop an application which will connect with .net webservice in order to retrieve data. I would like to make the ksoap2 call with async task. How I call it asyncronus with asynctask?
My SoapCall class is
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;
}
}
So far I am getting the response by calling the connection method in main activity with
SoapCall call1= new SoapCall();
call1.connection("get_clients", "%");
I'd suggest you use the AsyncTaskLoader which for my taste is easier than the AsyncTask. Have a look here, the example is very extensive and looks intimidating at first, you'll probably find much simpler ones. The idea is that your Activity implements
LoaderCallbacks
for the creation of the loader and a method that is being called when the loader has finished. You 'start' a loader via theLoaderManager
. The AsynctaskLoader is a class thatextends AsyncTaskLoader
and does the asynchronous stuff.I'll give you a simple example:
This is the AsyncTaskLoader:
This is what you have in the Activity that will start the loader, it is an inner class:
And this is how you start the loader from whereever you want (within that Activity):
where 0 is an ID that you give the loader, null is a Bundle of arguments. Good luck :)
Using asynctask is straightforward. Here is an example.
}
And the call to the task with parameters.
Hope it will help.