Symbian学习笔记(17) - 初探WebServices API的使用(上)

xiaoxiao2021-03-01  26

很久没有学习Symbian了,今天研究一下如何使用Symbian中提供的WebService框架来SayHello。

从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描述如下:

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; };

除了实现两个父类的方法以外,还要增加ConnectL()用来连接,SayHello()用来调用远程方法。那个delegate是一个CHelloWorldResult类的实例,这个类同样派生于CSenDomFragment,说明它对应一段XML内容,我们用它来处理结果,就是那个HelloWorldResponse标签下的内容。

这个WebEngine的实现逻辑是:先在ConnectL中初始化WS客户端,在SetStatus回调中取当前状态值如果为KSenConnectionStatusReady ,则可以调用SayHello去执行那个WS的方法,然后,在HandleMessageL回调中将得到的结果(XML内容的字节流)去解析一下,解析XML的回调就是那两个StartElement和EndElement。

附:这段代码太长了,分节吧。现在也不知怎么搞的,帖代码如此难看,唉。无语。

相关资源:微信小程序源码-合集1.rar
转载请注明原文地址: https://www.6miu.com/read-3350119.html

最新回复(0)