在一次項目開發過程中,用到IOS調用WebService接口,所以抽個空把這方面的內容給大家整理出來,分享給大家。
方法一:使用WSDL2ObjC工具,將接口轉成OC類。
1.在浏覽器中輸入webService接口地址(Safari不可用,我用的是Firefox),如:http://xxx.xxx.asmx, 地址後面添加上.wsdl成http://xxx.xxx.asmx.wsdl打開。
2.將頁面另存為wsdl文件,保存的時候後綴加上.wsdl,保存成如xxxxService.asmx.xml.wsdl。
3.使用WSDL2ObjC工具將wsdl文件轉成OC類。
4.將生成的OC類的全部文件導入到項目中。然後就可以調用了。現在沒有寫Demo,我就貼一部分調用代碼,以後有機會傳個Demo上來。
- (NSString *)skHkshListOfpagenow2:(NSInteger)aPagenow pagesize:(NSInteger)aPagesize { MURPXzshServiceSoapBinding *binding = [[MURPXzshServiceSoapBinding alloc]initWithAddress:[NSString stringWithFormat:@"%@%@", self.jjjj, XZSH_SERVICE]];//接口地址 binding.logXMLInOut = YES;//可以直接copy MURPXzshService_HkshList *parm = [[MURPXzshService_HkshList alloc] init];//接口中方法初始化 parm.xxxx = [umcid stringValue];//接口中方法傳參數 parm.pagenow = [NSNumber numberWithInteger:aPagenow];//接口中方法傳參數 parm.pagesize = [NSNumber numberWithInteger:aPagesize];//接口中方法傳參數 MURPXzshServiceSoapBindingResponse *resp = [binding HkshListUsingParameters:parm];//調用方法 //下面是返回值 for (id mine in resp.bodyParts) { if ([mine isKindOfClass:[MURPXzshService_HkshListResponse class]]) { NSString *resultStr = [mine HkshListResult]; return resultStr; } } return nil; }
注意:上面我的webService名字是MURPXzshService,Soap是MURPXzshServiceSoapBinding,調用的方法是HkshList。
方法二:直接通過網絡請求調用,解析XML的方法。
不說了,先上代碼:
- (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; //假如在這開始請求 NSString *webServiceBodyStr = [NSString stringWithFormat: @"<Jsjy_yjy xmlns=\"http://murpcn.com/murpwebservice/\">" "<xxx>34192</xxx>" "<pagenow>1</pagenow>" "<pagesize>20</pagesize>" "</Jsjy_yjy>"];//這裡是參數 NSString *webServiceStr = [NSString stringWithFormat: @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" "<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/\">\n" "<soap:Body>\n" "%@\n" "</soap:Body>\n" "</soap:Envelope>", webServiceBodyStr];//webService頭 NSString *SOAPActionStr = [NSString stringWithFormat:@"http://xxx.com/murpwebservice/%@", @"Jsjy_yjy"];//SOAPAction NSURL *url = [NSURL URLWithString:@"http://xxxx/key/MurpjsjyService.asmx"]; NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url]; NSString *msgLength = [NSString stringWithFormat:@"%ld", webServiceStr.length]; [theRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-type"]; [theRequest addValue:SOAPActionStr forHTTPHeaderField:@"SOAPAction"]; [theRequest addValue:msgLength forHTTPHeaderField:@"Content-Length"]; [theRequest setHTTPMethod:@"POST"]; [theRequest setHTTPBody:[webServiceStr dataUsingEncoding:NSUTF8StringEncoding]]; NSURLConnection *theConn = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; if (theConn) { NSLog(@"8888哈哈哈笑"); }else { NSLog(@"5555你給我哭"); } } //接收到數據的代理 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"%@", str);//得到想要的XML字符串然後解析 //系統自帶的 NSXMLParser *par = [[NSXMLParser alloc] initWithData:[str dataUsingEncoding:NSUTF8StringEncoding]]; [par setDelegate:self];//設置NSXMLParser對象的解析方法代理 [par parse];//調用代理解析NSXMLParser對象,看解析是否成功 } //解析XML #pragma mark xmlparser //step 1 :准備解析 - (void)parserDidStartDocument:(NSXMLParser *)parser{ } //step 2:准備解析節點 - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{ NSLog(@"%@", NSStringFromSelector(_cmd) ); } //step 3:獲取首尾節點間內容 - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{ NSLog(@"%@", string); } //step 4 :解析完當前節點 - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{ NSLog(@"%@",NSStringFromSelector(_cmd) ); } //step 5;解析結束 - (void)parserDidEndDocument:(NSXMLParser *)parser{ } //獲取cdata塊數據 - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock{ }
以上內容就是用IOS調用WebService(SOAP接口)的方式,希望對大家有所啟迪。