1.通过WSDL2ObjC调用WebService接口
利用 WSDL2ObjC 这个工具直接转换为OC类
第一个为WebService的地址 第二个为生成的OC类存放的位置 然后点击Parse WSDL 就OK了
生成文件如下图(因为服务不一样生成的第一个文件名会不同):
将生成的类直接拖入工程就可以用了
生成的文件一般为MRC 需要转为ARC
可能出现的错误 “libxml/tree.h” file not found
解决方法
TARGETS->Build Settings->Linking->Other Linker Flags 设置"-lxml2"
TARGETS->Build Settings->Search Paths->Header Search Paths 设置"/usr/include/libxml2"
TARGETS->Build Settings->Apple LLVM5.0-Language-Objective-Objective C->Objective-C Automatic Reference Counting 设置"NO"
(注意一旦服务增加新的方法或者更改参数、添加属性就需要重新更新服务)
2.接下来就是请求数据了
头文件包含生成方法的类 在我这里是MobileHiSService.h 遵守<MobileHISServiceSoap12BindingResponseDelegate> 协议 (对应的协议)
实现协议中方法
- (void)operation:(MobileHISServiceSoap12BindingOperation *)operation completedWithResponse:(MobileHISServiceSoap12BindingResponse *)response
这个方法就是服务返回的响应
MobileHISService_GetAllKeyBoard *request = [[ MobileHISService_GetAllKeyBoard alloc] init]; //GetAllKeyBoard为服务的方法名
request.TemplateID = @"1"; // 服务中规定的参数
MobileHISServiceSoap12Binding *binding = [[MobileHISServiceSoap12Binding alloc] initWithAddress:@"http://172.18.98.40:8080/MobileHISService.asmx"]; // 服务的地址
binding.defaultTimeout = 20; // 默认20s后超时
[binding GetAllKeyBoardAsyncUsingParameters:request delegate:self]; // 调用服务的方法()
再举个例子 这里有个公共的WebService接口
http://webservice.36wu.com/weatherService.asmx
首先需要在WSDL2Objc 工具的第一行中输入http://webservice.36wu.com/weatherService.asmx?wsdl (注意后面要改为?wsdl)
然后再生成OC类 如下图
然后拖入工程中
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
WeatherService_getExponentByCityName *request = [[WeatherService_getExponentByCityName alloc] init];
request.CityName = @"beijing";
request.UserId = @"";
WeatherServiceSoap12Binding *binding = [[WeatherServiceSoap12Binding alloc] initWithAddress:@"http://webservice.36wu.com/weatherService.asmx"];
[binding getExponentByCityNameAsyncUsingParameters:request delegate:self];
}
- (void)operation:(WeatherServiceSoapBindingOperation *)operation completedWithResponse:(WeatherServiceSoapBindingResponse *)response
{
}