Anda di halaman 1dari 6

• Article
• Comment
• View source
• History

Category: Symbian C++


This page was last modified on 11 March 2009, at 10:34.

Using gsoap for web services


From Forum Nokia Wiki

With the time-to-market being so critical for mobile applications, One would certainly like to
have as much reusable components as possible. This is where generic components come into
picture.

Contents
[hide]

• 1 Problem statement
• 2 What is gSOAP
• 3 Why gSOAP
• 4 How to use gSOAP
o 4.1 Step1: Generating the header file
o 4.2 Step2: Generating C++ code
o 4.3 Step3: Include generated file to your project directory

• 5 Multithreading for non-blocking calls

Problem statement
Let us say you want to design a S60 application with SOAP/XML web services communication.
Nokia provides a WSDL-to-C++ tool to generate stubs from wsdl file and Xmldatabinding
libraryto run this code. However, later if you decide to port this application to UIQ( or even
Samsung S60 devices), you need to re-design your application as this library is provided by
Nokia and doesn't work on other platforms or even S60 devices from other manufacturers. How
to avoid this problem by making the SOAP communication generic for all platforms. The answer
is gsoap.
What is gSOAP
A cross-platform open source C and C++ software development toolkit. Generates C/C++ RPC
code, XML data bindings, and efficient schema-specific parsers for SOAP Web services.

Why gSOAP
The primary reason to go for gSOAPis portability.gSOAP supports most platforms, including
embedded systems and small OS (for example WinCE, Symbian, and PalmOS).

How to use gSOAP


Download the latest gSOAP package from the SourceForge gSOAP project site. The gSOAP
distribution package includes two compiler tools to develop your applications:

'wsdl2h' WSDL parser: This tool converts WSDLs and XSD files into annotated C/C++
definitions.

'soapcpp2' stub and skeleton compiler :This tool generates RPC code and XML serializers
from the annotated C/C++ definitions.

The 'wsdl2h' parser converts WSDL into gSOAP header file specifications of Web services. This
specification gives a C/C++ transparent view of the server's functionality. The header file is
processed by 'soapcpp2' to generate the source code stubs and skeletons to invoke the service or
build a new service based on the WSDL.

Step1: Generating the header file


The first step is to generate the header file from your wsdl file using the wsdl2h tool. We may
need to modify the typemap.dat file to include the namespaces corresponding to our wsdl. If the
namespace is unspecified, the wsd2l tool will use the default ns1...ns2 values. In essence a
namespace aims to provide unique means to identify a type.

wsdl2h -s -t typemap.dat -o soapProxy.h mywebservice.wsdl

-s indicates that code that relies on STL should not be generated since the
STL is not available for Symbian.

-o specifies the output name of the file;

-t specifies the Specific typemap.dat to be used.

for more options, use wsdl2h -help


Step2: Generating C++ code
We need to run the soapProxy.h through the gSOAP compiler in order to create the proxy,
serializers and namespace mapping file.

The soapcpp2.exe is used with the following parameters:

soapcpp2 -t -C -L soapProxy.h

-t indicates that code is to generate typed messages (with the xsi:type


attribute).This helps ensure interoperability and compatibility when using
RPC-encoded style.

-C indicates that only client side code should be generated.

-L By default the compiler will attempt to create a soapClientLib.cpp which


is relevant for building static or dynamic libraries. This is not relevant to
us and is therefore disabled with the -L flag.

Step3: Include generated file to your project directory


Files
FileName Description inc src
soapProxy.h Defines schema types, services & bindings X
Amended schema types, services & binding
soapStub.h X
definitions
soapH.h XML (de)serializers for data types X
stdsoap2.h The gSOAP runtime X
myWebServiceSOAP11BindingProxy.h Proxy to invoke the services X
myWebServiceSOAP12BindingProxy.h Proxy to invoke the services X
soapC.cpp X
soapClient.cpp X
stdsoap2.cpp X

Multithreading for non-blocking calls


To keep the application UI responsive, We can issue the gsoap call in a seperate thread.

void CMyUpdater::StartL()
{
TInt id = iThread2.Handle ();
if ( (id==KCurrentThreadHandle)||(id==0))
{

//Thread uses shared heap created within main thread


TInt err = iThread2.Create
(_L("myupdater"),ThreadFunction,KDefaultStackSize,NULL,(TAny *)this);
if ( err==KErrNone)
{
iStatus=KRequestPending;
SetActive ();
iThread2.Resume ();
}
}
}

// protected from CActive


void CMyUpdater::DoCancel()
{
TInt id = iThread2.Handle ();
if ( (id!=KCurrentThreadHandle)&&(id!=0))
{
User::After (1000000);
/*Thread request must be indicated as completed
* when canceling the thread else the thread is blocked
* due to 'User::WaitForRequest()' in 'cancel()'
*
*/
TRequestStatus* status = &iStatus;
iThread2.RequestComplete(status, KErrCancel);
iThread2.Kill (KErrCancel);
iThread2.Close ();

}
}

void CMyUpdater::RunL()
{
if ( iStatus==KErrDied || KErrCancel)
{
//indicate the webservice response is processed - Ani
iThread2.Close ();
}
else
{
SetActive ();
}

// private new methods


TInt CMyUpdater::ThreadFunction(TAny *aPtr)
{
CMyUpdater* ptr = (CMyUpdater*)aPtr;

// open main thread


RThread thread;
thread.Open (ptr->iId, EOwnerProcess);
//Create a cleanup stack
CTrapCleanup* cleanup = CTrapCleanup::New ();
//Create and install a active scheduler
CActiveScheduler* sched(NULL);
sched=new CActiveScheduler;
if ( !sched)
{
delete cleanup;
return KErrNoMemory;
}

CActiveScheduler::Install (sched);

TRAPD(err,ptr->IssueRequestL());

CloseSTDLIB();

TRequestStatus* status = &(ptr->iStatus);


thread.RequestComplete (status, KErrDied);

//Delete the heap allocated stuff


delete sched;
delete cleanup;
return KErrNone;

void CMyUpdater::IssueRequestL()
{
//Issue web service request here and check for response
}
Retrieved from "http://wiki.forum.nokia.com/index.php/Using_gsoap_for_web_services"

Related Wiki Articles

Connect to webservice On android

1. private static final String METHOD_NAME = "TopGoalScorers";


2.
3. private static final String SOAP_ACTION =
"http://footballpool.dataaccess.eu/data/TopGoalScorers";
4.
5. private static final String NAMESPACE =
"http://footballpool.dataaccess.eu";
6.
7. private static final String URL =
"http://footballpool.dataaccess.eu/data/info.wso?WSDL";
8.
9. //you can get these values from the wsdl file^
10.
11. public SoapObject soap(String METHOD_NAME, String
SOAP_ACTION, String NAMESPACE, String URL) throws
IOException, XmlPullParserException {
12. SoapObject request = new SoapObject(NAMESPACE,
METHOD_NAME); //set up request
13.
14. request.addProperty("iTopN", "5"); //variable
name, value. I got the variable name, from the wsdl file!
15. SoapSerializationEnvelope envelope = new
SoapSerializationEnvelope(SoapEnvelope.VER11); //put all
required data into a soap envelope
16.
17. envelope.setOutputSoapObject(request); //prepare
request
18. AndroidHttpTransport httpTransport = new
AndroidHttpTransport(URL);
19.
20. httpTransport.debug = true; //this is optional,
use it if you don't want to use a packet sniffer to check
what the sent message was (httpTransport.requestDump)
21. httpTransport.call(SOAP_ACTION, envelope); //send
request
22. SoapObject
result=(SoapObject)envelope.getResponse(); //get response
23. return result;
24. }
25.
26. //usage:
27. //SoapObject result=soap(METHOD_NAME, SOAP_ACTION,
NAMESPACE, URL);
28. //don't forget to catch the exceptions!!

Nguon: http://www.helloandroid.com/tutorials/using-ksoap2-android-and-parsing-
output-data

Anda mungkin juga menyukai