Introduction
When developing Android app, I needed to call a web service. There are a lot
of libraries which permit to do this, but I have found in all of them some bugs
or too much referred dependencies (for example k2soap) and after use of them, i
preferred to develop myself.
If you respect some little rules, you can use a few lines of code to do a
full communication between a client Android and a soap web service.
Background
The general idea is how to have a library project of data entities to use
into both the projects: the web service and the client. If you need this
architectural structure, this library is one of the possible solutions.
Using the Code
Prerequisites
You need to add reference to commons-lang3-3.1.jar (download at
http://commons.apache.org/proper/commons-lang/download_lang.cgi).
Project is based on only the three source classes: DataTypeEnum.java,
Utils.java and the SoapHelper.java.
Usage
I've written the source based on "Entity
" classes which are the
content of the request and response messages.
Actually in this version, you have some limits in writing your entities:
- Use
public
properties - For
dateTime
, use the type "Calendar
" - Use package message for the request and response (explained later)
- Indicate to SoapHelper the various
List<...>
used into the object
received from service - The web methods of the service must use a unique parameter (an object
which contains data) named "
request
"
You can make a library of entities and reference it both in the web service
project and in the Android client project.
Suppose you have made two messages for the request and the response which
contains an Entity: you want to sent request and receive response:
Your web service must have only one request object which var
name is
"request
" as follows (if Java):
@WebMethod(operationName = "YourMethodName")
public msgYourMethodNameResponse YourMethodName(@WebParam(name =
"request") msgYourMethodNameRequest request) {
msgYourMethodNameResponse ret = new
msgYourMethodNameResponse();
return ret;
}
Or (if WCF):
[OperationContract]
msgYourMethodNameResponse YourMethodName(msgYourMethodNameRequest request);
Your data entitiy type class will implement Serializable
and will have
public
properties.
Example:
import java.io.Serializable;
import java.util.Calendar;
import java.util.UUID;
public class cBox implements Serializable {
public Integer _id;
public UUID uid;
public String boxCode;
public String boxDescription;
public Calendar dcreationdate;
public Calendar ddestroydate;
public double startLat;
public double startLon;
public double endLat;
public double endLon;
public cBox() {
this._id = 0;
this.uid = UUID.randomUUID();
this.boxCode = "";
this.boxDescription = "";
this.dcreationdate = Calendar.getInstance();
this.ddestroydate = null;
this.startLat = 0;
this.startLon = 0;
this.endLat = 0;
this.endLon = 0;
}
}
And if you need to use collection List
, you will have to write an
entity class like this (example):
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
public class cBoxData implements Serializable {
public cBox box;
public List<cPackData> packData;
public cBoxData()
{
box = new cBox();
packData = new ArrayList<cPackData>();
}
}
And now the core classes (download zip):
First class "EnumDataType
" is an enumeration of primitive types
plus three new types: "bytes
" to upload and download files,
"Object
" to tell SoapHelper
that the value to serialize/deserialize is
not a primitive type and "List
" to tell SoapHelper
that the item is
of type List<something>
.
Second class, "Utils
", contains common methods to convert
types from and into string
s or objects; there are also some methods to convert
Android database resultset if you wish to use it.
The third class SoapHelper
contains the methods to convert XML string into
Object, Object into XML string and to call the web service.
Ok, now you can use SoapHelper
in the Android client as follows
(substitute the word YourMethodNameToCall
with your own):
private static final String NAMESPACE =
"http://your.namespace.com/";
private static final String METHOD_NAME =
"YourMethodNameToCall";
private static final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
msgYourMethodNameRequest msgRequest = new msgYourMethodNameRequest();
msgYourMethodNameResponse msgResponse = new msgYourMethodNameResponse();
"data":
msgRequest.data = YourObjectEntityDataToSend;
SoapHelper sh = new SoapHelper(this, "ns2");
string
during transformation of returning xml:
object which will be used as clone to populate the collection:
sh.setInfoListClass("packData", new cPackData());
String envelope = sh.getXmlFromObject(METHOD_NAME, NAMESPACE, msgRequest);
String URL = YourWebServiceAddress;
envelope = sh.CallWebService(URL, SOAP_ACTION, envelope);
msgResponse = (msgYourMethodNameResponse) sh.getObjectFromXml(msgResponse,
envelope);
Surely you will be like to modify the source code to accept other types of data
(type "Date" for example instead of Calendar) or to use private fields
instead of public field in the entities (modify Utils with
"field.setAccessible(true)" when getting or setting properties, so
you can use standard java classes with getter and setter)
If you want to participate, please go to https://code.google.com/p/soapmanager.