iOS 基于WebService开发

来源:互联网 发布:xss攻击防御 php 编辑:程序博客网 时间:2024/05/17 22:24

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 = [[MobileHISServiceSoap12Bindingalloc] initWithAddress:@"http://172.18.98.40:8080/MobileHISService.asmx"];                          //服务的地址

    binding.defaultTimeout = 20;                                                             // 默认20s后超时

   

[binding GetAllKeyBoardAsyncUsingParameters:requestdelegate:self];                          //调用服务的方法()


再举个例子 这里有个公共的WebService接口

http://webservice.36wu.com/weatherService.asmx

首先需要在WSDL2Objc 工具的第一行中输入http://webservice.36wu.com/weatherService.asmx?wsdl  (注意后面要改为?wsdl)

然后再生成OC类 如下图

  

然后拖入工程中

- (void)viewDidLoad

{

    [superviewDidLoad];

    self.view.backgroundColor = [UIColorredColor];

    WeatherService_getExponentByCityName *request = [[WeatherService_getExponentByCityNamealloc] init];

    request.CityName = @"beijing";

    request.UserId = @"";

    WeatherServiceSoap12Binding *binding = [[WeatherServiceSoap12Bindingalloc] initWithAddress:@"http://webservice.36wu.com/weatherService.asmx"];

    [binding getExponentByCityNameAsyncUsingParameters:requestdelegate:self];

}


- (void)operation:(WeatherServiceSoapBindingOperation *)operation completedWithResponse:(WeatherServiceSoapBindingResponse *)response

{

    

}


0 0