Symbian學習筆記(17)
從SDK文檔中提供的資料來看這個接口似乎有點復雜,包括了Connection API、Description API和Manager API三套東西,此外還涉到了XML的解析之類的一些API的應用。
閱讀了一下它的例子程序(S60Ex目錄下的AddressBook),讓我更暈乎了。怎么跟自己平時使用的WebService不一樣了?
在SDK文檔中關于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做一個簡單的WebServices來測試,就用缺省產生的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>
下面我們自己來做一個WS的客戶端實例吧。先用向導生成一個HelloWorld應用,為了研究方便,我們不打算做什么界面,所有的輸出都通過LOG輸出到日志文件。
為了編碼方便,我們增加一個類WebEngine,它應該派生于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;
};
除了實現兩個父類的方法以外,還要增加ConnectL()用來連接,SayHello()用來調用遠程方法。那個delegate是一個 CHelloWorldResult類的實例,這個類同樣派生于CSenDomFragment,說明它對應一段XML內容,我們用它來處理結果,就是那個HelloWorldResponse標簽下的內容。
這個WebEngine的實現邏輯是:先在ConnectL中初始化WS客戶端,在SetStatus回調中取當前狀態值如果為 KSenConnectionStatusReady ,則可以調用SayHello去執行那個WS的方法,然后,在HandleMessageL回調中將得到的結果(XML內容的字節流)去解析一下,解析 XML的回調就是那兩個StartElement和EndElement。
【編輯推薦】