In this tutorial post, you will learning how to parsing a list of soap objects were responsed form a web service by using k-soap2 library and showing them to view after all.
1. Download and import library
- Launch Android Studio and start a new project.
- Download lastest k-soap2 jar file from Project's download page.
- Put yout jar file to your project - see this article for how to import external jar it into Android Studio project.
2. Web service preview
Okey, we have a dotNet web service like this:
As you can see, client must transfer 3 properties for each request (appType (int), userName (String) and sessionId(String)). Invoke a test after enter inputs, we have this result:
Now, we start making an Android project which consuming it.
3. Coding project core
After importing successfull library, we start coding for our project. In this example, we will make a request to server and retrieve a list of Banks from it.
For call a soap method using ksoap we need these 4 things (URL ADRESS, SOAP ACTION ADRRESS, NAMESPACE, METHOD NAME. Soap Action = Namespace + Method name). Define these properties value in ConstantString.java:
Make a model BankInfo with some information:For call a soap method using ksoap we need these 4 things (URL ADRESS, SOAP ACTION ADRRESS, NAMESPACE, METHOD NAME. Soap Action = Namespace + Method name). Define these properties value in ConstantString.java:
For making request to server, we'll use an AsyncTask and run in background thread. In it:
- Checking Internet connection (in onPreExcute method)
- Creating and set properties value for our SoapObject, create HttpRequest, Envelop, sending request and receiving response data in doInbackground method.
- processing data and call back to UI thread (MainActivity) in onPostExcute method.
getServiceResult method (creating, sending request and receive data):
Full GetBankTask.java source code:
In our activity, the most important method is calling back data from AsyncTask, parsing list of Soap Objects here, get each soap object property by getPrimitivePropertyAsString method:
Full MainActivity.java code:
4. Creating some necessary files
- Layout for activity:
- local build.gradle file:
- Strings resource:
- open AndroidManifest.xml add adding permissions to checking network state and using Internet: