最近因为个人原因,当然也有些迷茫,不过慢慢地就恢复正常了,开始继续学习IOS了,今天凌晨一点地WWDC,好像Apple的IOS7完全扁平化了,诶,看来Cook同学还是更加注重市场啊,嗯,话不多话,咱们今天来学习下xml解析查询火车票的信息,即xml通信。
从网上找了个可以获取火车票信息,网址是:http://webservice.webxml.com.cn/WebServices/TrainTimeWebService.asmx/getStationAndTimeByTrainCode?TrainCode=车次编号&UserID=
首先我们设计了如下的一个xib视图:
这里我们使用了一个第三方的xml解析库GDataXMLNode
,虽然IOS自带的SDK库中也有相应的处理xml文件的类,但好像第三方的一些类库更佳简洁高效,我们看下它的使用方法。
在我们自己的类中引用该类库,然后我们来实现下NSUrlConnection的几个方法:
#define kHttpAddress1 @"http://webservice.webxml.com.cn/WebServices/TrainTimeWebService.asmx/getStationAndTimeByTrainCode?TrainCode=" #define kHttpAddress2 @"&UserID=" -(void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse *)response{ NSLog(@"%@receiveResponse.\n",self); } -(void)connection:(NSURLConnection*)connection didReceiveData:(NSData *)data{ NSLog(@"%@ didReceiveData\n",self); NSString* strPartData=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"=== %@", strPartData); [self.strReceivedData appendString:strPartData]; }
//connection连接完成获取数据结束 -(void)connectionDidFinishLoading:(NSURLConnection*)connection{ NSLog(@"%@ connectionDidFinish.\n",self); //通过字符串内容创建,GDataXMLDocument实例 NSError*error = nil; GDataXMLDocument*doc = [[[GDataXMLDocument alloc]initWithXMLString:self.strReceivedData options:0 error:&error]autorelease]; if(error) { NSLog(@"Create GDataXMLDocument error,error code: %@",[error localizedDescription]); return; } //首先查找到根元素 GDataXMLDocument* rootElement = [doc rootElement]; //在根元素里面查找所有NAME为“Children"的子元素 NSArray*arrStudentElement = [rootElement elementsForName:@"string" ]; // //创建接收解释结果的可变字符串 self.strResult= nil; self.strResult = [NSMutableString stringWithCapacity:100]; [self.strResult appendString:@"车次信息" ]; [self.strResult appendString:@"\r\n"]; for (GDataXMLNode*node in arrStudentElement) { for (GDataXMLNode*subNode in node.children) { [self.strResult appendString:subNode.stringValue]; [self.strResult appendString:@"\r\n"]; NSLog(@"%@",subNode.stringValue); } // [self.strResult appendString:@"\r\n"]; } [self.strResult appendString:@"解析完成"]; self.textView.text = self.strResult; NSLog(@"%@",self.strResult); [connection release]; connection=nil; [self.strReceivedData release]; self.strReceivedData=[[NSMutableString alloc]initWithCapacity:100]; } //如果连接失败 -(void)connection:(NSURLConnection*)connection didFailWithError:(NSError *)error{ NSLog(@"Fetch error%@",error.localizedDescription); [connection release]; connection=nil; [self.strReceivedData release]; self.strReceivedData=[[NSMutableString alloc]initWithCapacity:0]; }
//点击按钮方法 - (IBAction)clickButton:(id)sender { NSString* strstart=self.lblstart.text; NSString* strUserid=@""; NSString* strurl1=[kHttpAddress1 stringByAppendingString:strstart]; NSString* strurl2=[kHttpAddress2 stringByAppendingString:strUserid]; NSString* strURL=[strurl1 stringByAppendingString:strurl2]; NSLog(@"%@",strURL); NSString* strURLLegal=[strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; if (!strURLLegal) { return; } NSURL* urlRequest=[NSURL URLWithString:strURLLegal]; NSURLRequest* request=[NSURLRequest requestWithURL:urlRequest cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60.0]; NSURLConnection* connection=[[NSURLConnection alloc ]initWithRequest:request delegate:self]; [connection start]; }OK,这样儿我们就完成了从远程地址查询xml并解析列车车次信息,运行下,效果如下:
但是很不幸的,被键盘给盖住了。。。所以我们还要添加如下的一个方法,实现点击return的时候隐藏键盘:
- (IBAction)resignTextFiled:(id)sender { [self.lblstart resignFirstResponder]; }
好了,今天先学到这里,换换脑子,写python去了。
2013年06月11日,Eric.Tang 记