以前用Java的时候,采用Ksoap2去访问webservice, 请问大侠们,在iphone上面,应该如何去访问webservice呢?
Thanks!

解决方案 »

  1.   

    给你一个我做过的案例吧是关于一个webservice的解析的 关键市解析xml文件,在苹果底下没有现成的类将xml文件解析成树状的类,自己按照帮助文档的案例推敲吧!#import "QQViewController.h"
    @implementation QQViewController@synthesize qqCodeText;
    @synthesize qqStatusLabel;@synthesize webData;
    @synthesize soapResults;
    @synthesize xmlParser;
    @synthesize recordResults;
    @synthesize activityIndicatorView;//处理文字输入完毕键盘的隐藏  或者在输入完毕按回车时直接进行查询
    -(IBAction)textDidEndExit:(id)sender{
    //[qqCodeText resignFirstResponder];
    [sender resignFirstResponder];
    }- (void)getQQStatus{
    recordResults=NO;

    //soap request message
    NSString *soapMessage=[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>"
       "<qqCheckOnline xmlns=\"http://WebXml.com.cn/\">"
       "<qqCode>%@</qqCode>"
       "</qqCheckOnline>"
       "</soap:Body>"
       "</soap:Envelope>",qqCodeText.text];
    //请求地址
    NSURL *url=[NSURL URLWithString:@"http://webservice.webxml.com.cn/webservices/qqOnlineWebService.asmx"];

    NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:url];

    NSString *msgLegth=[NSString stringWithFormat:@"%d",[soapMessage length]];

    [theRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [theRequest addValue:@"http://WebXml.com.cn/qqCheckOnline" forHTTPHeaderField:@"SOAPAction"];


    [theRequest addValue:msgLegth forHTTPHeaderField:@"Content-Length"];
    [theRequest setHTTPMethod:@"POST"];
    [theRequest setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding ]];

    //request
    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    //connection
    if(theConnection){
    webData=[[NSMutableData data]retain];
    }else{
    NSLog(@"theConnection is NULL");
    }

    }-(IBAction)selectStatus{
    //qqStatusLabel.text=@"Getting time ...";

    //等待界面
    [activityIndicatorView startAnimating];

    [qqCodeText resignFirstResponder];//为什么在这里释放

    [self getQQStatus];
    }
    - (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
        [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
    }- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;

    self.qqCodeText=nil;
    self.qqStatusLabel=nil;

    [super viewDidUnload];
    }- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }- (void)dealloc {
    [qqCodeText release];
    [qqStatusLabel release];

        [super dealloc];
    }//接受到数据
    -(void)connection:(NSURLConnection *)connection
       didReceiveData:(NSData *)data{
    [webData appendData:data];
    NSLog(@"connection didReceiveData:2");
    }//没有接受到数据
    -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    /* This method is called when the server has determined that it has
     enough information to create the NSURLResponse. It can be called
     multiple times, for example in the case of a redirect, so each time
     we reset the data. */

    NSLog(@"webdata length is :%d",[webData length]);
    [webData setLength:0];
    NSLog(@"connection:didReceiveResponse:1");
    }//
    -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    NSLog(@"ERROR with theConnection");
    [connection release];
    [webData release];
    }-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
    NSString *theXML=[[NSString alloc] 
      initWithBytes:[webData mutableBytes] 
      length:[webData length]
      encoding:NSUTF8StringEncoding];

    [theXML release];
    if(xmlParser){
    [xmlParser release];
    }

    xmlParser=[[NSXMLParser alloc] initWithData:webData];
    [xmlParser setDelegate:self];
    [xmlParser setShouldResolveExternalEntities:YES];
    [xmlParser parse];

    [connection release];
    }-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
       attributes:(NSDictionary *)attributeDict{
    if([elementName isEqualToString:@"qqCheckOnlineResult"]){
    if(!soapResults){
    soapResults=[[NSMutableString alloc] init];
    }
    recordResults=YES;
    }
    }-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    if(recordResults){
    [soapResults appendString:string];
    }
    }//在这里接收到返回的数据
    -(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{

    if([elementName isEqual:@"qqCheckOnlineResult"]){
    recordResults=FALSE;

    /*处理结果Y = 在线;N = 离线;E = QQ号码错误;A = 商业用户验证失败;V = 免费用户超过数量*/

    [activityIndicatorView stopAnimating];
    if([soapResults isEqualToString:@"Y"]){
    qqStatusLabel.text=[[[NSString init] stringWithFormat:@"%@ 状态是 :",qqCodeText.text] stringByAppendingString:@"在线"];
    }else if([soapResults isEqualToString:@"N"]){
    qqStatusLabel.text=[[[NSString init] stringWithFormat:@"%@ 状态是 :",qqCodeText.text] stringByAppendingString:@"离线"];
    }else if([soapResults isEqualToString:@"E"]){
    qqStatusLabel.text=[[[NSString init] stringWithFormat:@"%@ 状态是 :",qqCodeText.text] stringByAppendingString:@"QQ号码错误"];
    }else if([soapResults isEqualToString:@"A"]){
    qqStatusLabel.text=[[[NSString init] stringWithFormat:@"%@ 状态是 :",qqCodeText.text] stringByAppendingString:@"商业用户验证失败"];
    }else if([soapResults isEqualToString:@"V"]){
    qqStatusLabel.text=[[[NSString init] stringWithFormat:@"%@ 状态是 :",qqCodeText.text] stringByAppendingString:@"免费用户超过数量"];
    }

    [soapResults release];
    soapResults=nil;
    }
    }-(void)parserDidStartDocument:(NSXMLParser *)parser{
    //解析开始
    //[activityIndicatorView ];
    }
    -(void)parserDidEndDocument:(NSXMLParser *)parser{
    //解析完成
    }
    -(void)viewDidLoad{
    [activityIndicatorView stopAnimating];
    [activityIndicatorView hidesWhenStopped];

    [super viewDidLoad];
    }@end