您的位置:首页 > 移动开发 > IOS开发

IOS 调用WebService(同步和异步)

2014-03-26 16:33 543 查看
因为公司的服务全都是webservice,每次总要花费大量时间在调试服务上面,干脆就写了一个解析wsdl的项目,希望将来能用上吧。还未经过烘焙,有问题,还请高手点播点播。

下面,我拿天气服务的wsdl作为例子吧。

服务的WSDL地址:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl

WSDL包含以下节点

definitions 根节点

根节点下面有以下节点:

types 数据类型定义。方法的参数名都包含在里面。

message 消息数据结构。

portType 描述服务和服务的方法。

binding 描述Web Service的通信协议。

service 描述Web Service 的访问点的集合。

下面对来一步一步解析如何根据wsdl 生成SOAP 消息体。

1.添加一个类扩展,如下图DDXMLElement+WSDL.h和DDXMLElement+WSDL.m



头文件中,暴露以下方法



2.SoapUtility 文件是用来封装soap消息的。SoapUtility调用DDXMLElement+WSDL

在SoapUtility头文件中,暴露以下方法



3.服务调用,上面,都把Soap消息给准备好了。那么最后一步就是服务的调用了。这里分两种调用方式:同步和异步。



4.使用方法,下面是天气服务的调用例子

//参数列表
NSDictionary *dic=@{@"theCityName": cityname};
//方法名
NSString *methodName=@"getWeatherbyCityName";
  
//封装soap信封
SoapUtility *soaputility=[[SoapUtility alloc] initFromFile:@"WeatherWebService"];
NSString *postData=[soaputility BuildSoapwithMethodName:@"getWeatherbyCityName" withParas:dic];
  
//初始化服务
SoapService *soaprequest=[[SoapService alloc] init];
soaprequest.PostUrl=@"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx";
soaprequest.SoapAction=[soaputility GetSoapActionByMethodName:methodName SoapType:SOAP];
  
if (isSync) {
//同步方法
ResponseData *result= [soaprequest PostSync:postData];
[self.result setText:result.Content];
}
else{
//异步请求
[soaprequest PostAsync:postData Success:^(NSString *response) {
[self.result setText:response];
} falure:^(NSError *response) {
[self.result setText:response.description];
}];
}

5.代码实现

https://github.com/xujialiang/SOAP-IOS

欢迎大家给意见。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  WebService