Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile / Android

Soap Manager

4.00/5 (1 vote)
10 Jan 2014CPOL2 min read 11.3K   95  
Library to send and receive objects between an android client and a web service

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:

  1. Use public properties
  2. For dateTime, use the type "Calendar"
  3. Use package message for the request and response (explained later)
  4. Indicate to SoapHelper the various List<...> used into the object received from service
  5. 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):

Java
// NOTE: param name is "request" this name
//is used into the SoapHelper as first argument to create the SOAP XML string and
//to translate objects received  

@WebMethod(operationName = "YourMethodName")
public msgYourMethodNameResponse YourMethodName(@WebParam(name =
"request") msgYourMethodNameRequest request) {
        msgYourMethodNameResponse ret = new
msgYourMethodNameResponse();
        // do something with request object
        return ret;
    }

Or (if WCF):

C#
[OperationContract]
msgYourMethodNameResponse YourMethodName(msgYourMethodNameRequest request); 

Your data entitiy type class will implement Serializable and will have public properties. 

Example:

Java
import java.io.Serializable;
import java.util.Calendar;
import java.util.UUID;
/**
 *
 * @author         Nicola Massari -
supernik69@gmail.com
 * @version 1.0
 * 
 * cBox entity class manage data from tbBox on the database
 * 
 */
public class cBox implements Serializable {
    /**
     * Id implementation for android database
     */
    public Integer _id;
    /**
     * Primary key
     */
    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;
    /**
     * Class constructor, set all data to a non null value
     */
    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):

Java
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author          Nicola Massari -
supernik69@gmail.com
* @version 1.0
* 
* cBoxData entity class to manage tbpack related to tbbox data on the database
*/ 
public class cBoxData implements Serializable {
          
    public cBox box;
    /**
     * List of cPackData
     */
    public List<cPackData> packData;
    /**
     * Class constructor, set all data to a non null value
     */
    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 strings 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):

Java
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;
// instantiate it:
msgYourMethodNameRequest msgRequest = new msgYourMethodNameRequest();
msgYourMethodNameResponse msgResponse = new msgYourMethodNameResponse();
//Now you have to do only the follow instructions:
//for example the request message contains an entity called
"data": 
msgRequest.data = YourObjectEntityDataToSend;
// instantiate SoapHelper       
SoapHelper sh = new SoapHelper(this, "ns2"); // a tag for xml soap
string
// tell to SoapHelper which are the type collection list that it can meet
during transformation of returning xml:
// this is done giving the name of vars which indicate the collection and a new
object which will be used as clone to populate the collection:
// for example: 
sh.setInfoListClass("packData", new cPackData()); 
// create the string xml:
String envelope = sh.getXmlFromObject(METHOD_NAME, NAMESPACE, msgRequest);
// call the service and get the string          
      
String URL = YourWebServiceAddress;
envelope = sh.CallWebService(URL, SOAP_ACTION, envelope);
// convert the xml into your object  
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)