Symbian學(xué)習(xí)筆記(17)
從SDK文檔中提供的資料來看這個(gè)接口似乎有點(diǎn)復(fù)雜,包括了Connection API、Description API和Manager API三套東西,此外還涉到了XML的解析之類的一些API的應(yīng)用。
閱讀了一下它的例子程序(S60Ex目錄下的AddressBook),讓我更暈乎了。怎么跟自己平時(shí)使用的WebService不一樣了?
在SDK文檔中關(guān)于CSenServiceConnection有這么一段描述:
Web Services包括兩種不同的框架模型:
1. Identity Based Web Services Framework (ID-WSF). The framework ID for this is KDefaultIdWsfFrameworkID ("ID-WSF").
2. Basic Web Services Framework. Framework ID is KDefaultBasicWebServicesFrameworkID ("WS-I").
如果提供了Contract則缺省使用ID-WSF。
首先用.NET做一個(gè)簡單的WebServices來測(cè)試,就用缺省產(chǎn)生的HelloWorld吧。很簡單的,它的SOAP描述如下:
<PRE class=csharp name="code">POST /uim/PService.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "urn:pservice:helloworld"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<HelloWorld xmlns="http://sharetop/pservice" />
</soap:Body>
</soap:Envelope>
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<HelloWorldResponse xmlns="http://sharetop/pservice">
<HelloWorldResult>string</HelloWorldResult>
</HelloWorldResponse>
</soap:Body>
</soap:Envelope></PRE>
view plaincopy to clipboardprint?
POST /uim/PService.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "urn:pservice:helloworld"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<HelloWorld xmlns="http://sharetop/pservice" />
</soap:Body>
</soap:Envelope>
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<HelloWorldResponse xmlns="http://sharetop/pservice">
<HelloWorldResult>string</HelloWorldResult>
</HelloWorldResponse>
</soap:Body>
</soap:Envelope>
POST /uim/PService.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "urn:pservice:helloworld"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<HelloWorld xmlns="http://sharetop/pservice" />
</soap:Body>
</soap:Envelope>
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<HelloWorldResponse xmlns="http://sharetop/pservice">
<HelloWorldResult>string</HelloWorldResult>
</HelloWorldResponse>
</soap:Body>
</soap:Envelope>
下面我們自己來做一個(gè)WS的客戶端實(shí)例吧。先用向?qū)梢粋€(gè)HelloWorld應(yīng)用,為了研究方便,我們不打算做什么界面,所有的輸出都通過LOG輸出到日志文件。
為了編碼方便,我們?cè)黾右粋€(gè)類WebEngine,它應(yīng)該派生于CSenBaseFragment和MSenServiceConsumer。聲明如下:
class CWebEngine : public CSenBaseFragment, public MSenServiceConsumer
{
public:
~CWebEngine();
static CWebEngine* NewL();
static CWebEngine* NewLC();
void ConnectL();
void SayHello();
//from MSenServiceConsumer
virtual void HandleMessageL(const TDesC8& aMessage);
virtual void HandleErrorL(const TInt aErrorCode,const TDesC8& aError);
virtual void SetStatus(const TInt aStatus);
protected:
//from CSenBaseFragment
virtual void StartElementL(const TDesC8& aNsUri, const TDesC8& aLocalName, const TDesC8& aQName, const Xml::RAttributeArray& aAttrs);
virtual void EndElementL(const TDesC8& aNsUri, const TDesC8& aLocalName, const TDesC8& aQName);
private:
CWebEngine();
void ConstructL();
public:
CHelloWorldResult * delegate;
private:
CSenServiceConnection* iConnection;
CSenXmlServiceDescription* iSession;
CSenXmlReader* iXmlReader;
};
class CWebEngine : public CSenBaseFragment, public MSenServiceConsumer
{
public:
~CWebEngine();
static CWebEngine* NewL();
static CWebEngine* NewLC();
void ConnectL();
void SayHello();
//from MSenServiceConsumer
virtual void HandleMessageL(const TDesC8& aMessage);
virtual void HandleErrorL(const TInt aErrorCode,const TDesC8& aError);
virtual void SetStatus(const TInt aStatus);
protected:
//from CSenBaseFragment
virtual void StartElementL(const TDesC8& aNsUri, const TDesC8& aLocalName, const TDesC8& aQName, const Xml::RAttributeArray& aAttrs);
virtual void EndElementL(const TDesC8& aNsUri, const TDesC8& aLocalName, const TDesC8& aQName);
private:
CWebEngine();
void ConstructL();
public:
CHelloWorldResult * delegate;
private:
CSenServiceConnection* iConnection;
CSenXmlServiceDescription* iSession;
CSenXmlReader* iXmlReader;
};
除了實(shí)現(xiàn)兩個(gè)父類的方法以外,還要增加ConnectL()用來連接,SayHello()用來調(diào)用遠(yuǎn)程方法。那個(gè)delegate是一個(gè) CHelloWorldResult類的實(shí)例,這個(gè)類同樣派生于CSenDomFragment,說明它對(duì)應(yīng)一段XML內(nèi)容,我們用它來處理結(jié)果,就是那個(gè)HelloWorldResponse標(biāo)簽下的內(nèi)容。
這個(gè)WebEngine的實(shí)現(xiàn)邏輯是:先在ConnectL中初始化WS客戶端,在SetStatus回調(diào)中取當(dāng)前狀態(tài)值如果為 KSenConnectionStatusReady ,則可以調(diào)用SayHello去執(zhí)行那個(gè)WS的方法,然后,在HandleMessageL回調(diào)中將得到的結(jié)果(XML內(nèi)容的字節(jié)流)去解析一下,解析 XML的回調(diào)就是那兩個(gè)StartElement和EndElement。
【編輯推薦】