Chinaunix首页 | 论坛 | 博客
  • 博客访问: 22229
  • 博文数量: 28
  • 博客积分: 670
  • 博客等级: 上士
  • 技术积分: 285
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-09 11:27
文章分类

全部博文(28)

文章存档

2011年(28)

我的朋友
最近访客

分类: 嵌入式

2011-01-29 12:23:19

(转载 --留着以后学习)

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

    

    
    

    

    

    

HTTP/1.1 200 OK     

Content-Type: text/xml; charset=utf-8     

Content-Length: length     

    

    

      

    
string   
  
    

  
    

  

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"   

    

     

     

     

     

     

HTTP/1.1 200 OK     

Content-Type: text/xml; charset=utf-8     

Content-Length: length    

     

     

       

     

string     

     

   

  

POST /uim/PService.asmx HTTP/1.1

Host: localhost

Content-Type: text/xml; charset=utf-8

Content-Length: length

SOAPAction: "urn:pservice:helloworld"


  
   
  


HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length


  
   
      string
   

  



下面我们自己来做一个WS的客户端实例吧。先用向导生成一个HelloWorld应用,为了研究方便,我们不打算做什么界面,所有的输出都通过LOG输出到日志文件。

为了编码方便,我们增加一个类WebEngine,它应该派生于CSenBaseFragment和MSenServiceConsumer。声明如下: 
view plaincopy to clipboardprint?

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。

阅读(304) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~