- 浏览: 93783 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
sincerehui:
android二维码的编码与解码(图片解码与摄像头解码) -
nyh1006:
学习一下!
UIScrollView循环滚动 -
bravewly:
为啥扫不出结果呢?是我扫的方式不对么?
android二维码的编码与解码(图片解码与摄像头解码) -
messigoogle:
我说您这个也是跟eoe上的差不多吧,加载的Assert中的图片 ...
android瀑布流 -
gundumw100:
LazyScrollView是偶写的。欢迎使用。呵呵。http ...
android瀑布流
使用的WebService是http://www.webxml.com.cn/WebServices/WeatherWebService.asmx
其中的getWeatherByCityName函数
常使用的技术有:SOAP,HTTP GET, HTTP POST三种方式
(一) SOAP(简单对象访问协议)方式
当你使用SOAP时,必须用到POST方式
(1) SOAP 1.1
以下是请求实例:
POST /WebServices/WeatherWebService.asmx HTTP/1.1 Host: www.webxml.com.cn Content-Type: text/xml; charset=utf-8 Content-Length: length SOAPAction: "http://WebXml.com.cn/getWeatherbyCityName" <?xml version="1.0" encoding="utf-8"?> <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/"> <soap:Body> <getWeatherbyCityName xmlns="http://WebXml.com.cn/"> <theCityName>string</theCityName> </getWeatherbyCityName> </soap:Body> </soap:Envelope>
请求方法:
- (void) soap1_1 { NSString *province=[NSString stringWithFormat:@"上海"]; //设置soap请求信息 NSString *soapString=[[NSString alloc] initWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>" "<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/\">" "<soap:Body>" "<getWeatherbyCityName xmlns=\"http://WebXml.com.cn/\">" "<theCityName>%@</theCityName>" "</getWeatherbyCityName>" "</soap:Body>" "</soap:Envelope>",province]; NSLog(@"%@",soapString); //soap请求地址 NSURL *url=[[NSURL alloc] initWithString:@"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx"]; //请求 NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url]; //设置请求头部 ////设置ContentType [request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; ////设置SOAPAction [request addValue:@"http://WebXml.com.cn/getWeatherbyCityName" forHTTPHeaderField:@"SOAPAction"]; //设置Content-length [request addValue:[NSString stringWithFormat:@"%d",[soapString length]] forHTTPHeaderField:@"Content-Length"]; //设置请求类型 POST或GET [request setHTTPMethod:@"POST"]; //设置请求Body(只有post方式有) [request setHTTPBody:[soapString dataUsingEncoding:NSUTF8StringEncoding]]; //连接 NSURLConnection *connection=[NSURLConnection connectionWithRequest:request delegate:self]; if (connection) { webData=[NSMutableData data]; } }
(2) SOAP 1.2(除了soap不一样外,其他都一样)
以下是请求实例:
POST /WebServices/WeatherWebService.asmx HTTP/1.1 Host: www.webxml.com.cn Content-Type: application/soap+xml; charset=utf-8 Content-Length: length <?xml version="1.0" encoding="utf-8"?> <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:Body> <getWeatherbyCityName xmlns="http://WebXml.com.cn/"> <theCityName>string</theCityName> </getWeatherbyCityName> </soap12:Body> </soap12:Envelope>
请求方法:
- (void) soap1_2 { NSString *province=[NSString stringWithFormat:@"上海"]; //设置soap请求信息 NSString *soapString=[[NSString alloc] initWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>" "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">" "<soap12:Body>" "<getWeatherbyCityName xmlns=\"http://WebXml.com.cn/\">" "<theCityName>%@</theCityName>" "<theCityName>%@</theCityName>" "</getWeatherbyCityName>" "</soap12:Body>" "</soap12:Envelope>",province]; NSLog(@"%@",soapString); //soap请求地址 NSURL *url=[[NSURL alloc] initWithString:@"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx"]; //请求 NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url]; //设置请求头部 ////设置ContentType [request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; ////设置SOAPAction [request addValue:@"http://WebXml.com.cn/getWeatherbyCityName" forHTTPHeaderField:@"SOAPAction"]; //设置Content-length [request addValue:[NSString stringWithFormat:@"%d",[soapString length]] forHTTPHeaderField:@"Content-Length"]; //设置请求类型 POST或GET [request setHTTPMethod:@"POST"]; //设置请求Body(只有post方式有) [request setHTTPBody:[soapString dataUsingEncoding:NSUTF8StringEncoding]]; //连接 NSURLConnection *connection=[NSURLConnection connectionWithRequest:request delegate:self]; if (connection) { webData=[NSMutableData data]; } }
其实和soap1.1除了soap不一样 ,几乎全一样
(二) HTTP POST方式
请求实例:
POST /WebServices/WeatherWebService.asmx/getWeatherbyCityName HTTP/1.1 Host: www.webxml.com.cn Content-Type: application/x-www-form-urlencoded Content-Length: length theCityName=string
方法:
- (void) httpPost { NSString *postString=@"theCityName=上海"; //此处的URL是POST /WebServices/WeatherWebService.asmx/getWeatherbyCityName 见上! NSURL *url=[NSURL URLWithString:@"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName"]; NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url]; [request addValue:@"application/x-www-form-urlencoded"forHTTPHeaderField:@"Content-Type"];//注意是中划线 [request addValue:[NSString stringWithFormat:@"%d",[postString length]] forHTTPHeaderField:@"Content-Length"]; [request setHTTPMethod:@"POST"]; [request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]]; NSURLConnection *connection=[NSURLConnection connectionWithRequest:request delegate:self]; if (connection) { webData=[NSMutableData data]; } }
以上三种任选一种都可以。下面开始写余下的程序代码了。
——————ViewController文件——————
#import <UIKit/UIKit.h> @interface WebServiceViewController : UIViewController<NSURLConnectionDelegate,NSURLConnectionDataDelegate> { NSMutableData *webData; NSMutableArray *parserObjects; } @property (strong, nonatomic) NSMutableData *webData; @property (strong, nonatomic) NSString *m_strCurrentElement; @property (strong, nonatomic) NSMutableString *tempString; @end
——————NSURLConnectionDelegate,NSURLConnectionDataDelegate实现方法——————
//接收相应的时候触发 -(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ [webData setLength:0]; } //接收数据的时候触发 -(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ [webData appendData:data]; } //全部完成时触发 -(void) connectionDidFinishLoading:(NSURLConnection *)connection{ NSString *dataString=[[NSString alloc] initWithBytes:[webData bytes] length:[webData length] encoding:NSUTF8StringEncoding]; NSLog(@"%@",dataString); //XML解析 NSXMLParser *xmlParser=[[NSXMLParser alloc] initWithData:webData]; [xmlParser setDelegate:self]; [xmlParser setShouldResolveExternalEntities:YES]; [xmlParser parse]; } //发生异常触发 -(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ NSLog(@"Error:%@",error); }
现在就应该能看到打印出来的返回数据了,我查了一些资料没找到webserive的解析,我只能用xml解析。
正好我可以学习一下xml的解析:
添加协议NSXMLParserDelegate
然后在
-(void) connectionDidFinishLoading:(NSURLConnection *)connection
方法里添加下面代码:
//XML解析 NSXMLParser *xmlParser=[[NSXMLParser alloc] initWithData:webData]; [xmlParser setDelegate:self]; [xmlParser setShouldResolveExternalEntities:YES]; [xmlParser parse];
—————— NSXMLParserDelegate实现方法——————
//开始时触发 - (void)parserDidStartDocument:(NSXMLParser *)parser { parserObjects = [[NSMutableArray alloc] init]; //每一组信息都用数组来存,最后得到的数据即在此数组中 } //解析xml标签对,开始时触发 - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict { NSLog(@">>>%@",elementName); if ([elementName isEqualToString:@"ArrayOfString"]) { //开始解析ArrayOfString节点 NSLog(@"开始"); }else { //开始解析子节点 if ([elementName isEqualToString:@"string"]) { self.m_strCurrentElement = @"string"; self.tempString = [NSMutableString string]; } } } //找到标签时 - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { NSLog(@"found %@",self.m_strCurrentElement); //填充string if (self.m_strCurrentElement) { [self.tempString appendString:string]; NSLog(@"found is %@",string); } } //解析xml标签对,结束时触发 - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { NSLog(@"end %@",self.m_strCurrentElement); //填充 if (self.m_strCurrentElement) { [parserObjects addObject:self.tempString]; self.m_strCurrentElement = nil; self.tempString = nil; } //结束解析ArrayOfString节点 if ([elementName isEqualToString:@"ArrayOfString"]) { NSLog(@"结束"); } } //结束时触发 - (void)parserDidEndDocument:(NSXMLParser *)parser { //由于标签对的名字都是一样的,只能这样获得 //天气图片 NSString *weatherImageName = [parserObjects objectAtIndex:8]; //今日天气实况 NSString *temperature = [parserObjects objectAtIndex:10]; NSString *imageName = [NSString stringWithFormat:@"a_%@",weatherImageName]; [self gifImageView:imageName]; [self.wendu setText:[self segmentation:temperature]]; }
由于今日天气实况的格式是:
今日天气实况:气温:26℃;风向/风力:北风 1级;湿度:78%;空气质量:良;紫外线强度:弱
有时只需要气温,这样我们就要分割这个字符串,分割这个字符串有两种方法:分段,正则表达式。
1.分段
#pragma 分割 - (NSString *)segmentation:(NSString *)segmentationString { //今日天气实况:气温:26℃;风向/风力:北风 1级;湿度:78%;空气质量:良;紫外线强度:弱 //取出 ————> 26℃ NSArray *segmentationArray1 = [segmentationString componentsSeparatedByString:@";"]; NSString *segmentationString1 = [segmentationArray1 objectAtIndex:0]; //今日天气实况:气温:26℃; NSArray *segmentationArray2 = [segmentationString1 componentsSeparatedByString:@":"]; NSString *segmentationResult = [segmentationArray2 objectAtIndex:2]; NSLog(@"segmentation : %@",segmentationResult); return segmentationResult; }
2.正则表达式
#pragma 正则表达式 - (void)regular:(NSString *)regularString { //今日天气实况:气温:26℃;风向/风力:北风 1级;湿度:78%;空气质量:良;紫外线强度:弱 //取出 ————> 26℃ NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[0-9]*℃" options:0 error:nil]; //系统通过自有类NSRegularExpression和NSTextCheckingResult来实现正则表达式的创建和筛选 if (regex != nil) { NSTextCheckingResult *firstMatch = [regex firstMatchInString:regularString options:0 range:NSMakeRange(0, [regularString length])]; if (firstMatch) { NSRange resultRange = [firstMatch rangeAtIndex:0]; //从regularString中截取数据 NSString *result = [regularString substringWithRange:resultRange]; NSLog(@"regular %@",result); } } }
这样就可以只显示气温了。
有时候气温图片需要gif图
显示gif图的方法:
-(void)gifImageView:(NSString *)imageName { NSLog(@"%@",imageName); // 设定位置和大小 CGRect frame = CGRectMake(50,50,0,0); frame.size = [UIImage imageNamed:imageName].size; NSArray *imageArray = [imageName componentsSeparatedByString:@"."]; // 读取gif图片数据 NSData *gif = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource:[imageArray objectAtIndex:0] ofType:[imageArray objectAtIndex:1]]]; // view生成 UIWebView *webView = [[UIWebView alloc] initWithFrame:frame]; webView.userInteractionEnabled = NO;//用户不可交互 [webView loadData:gif MIMEType:@"image/gif" textEncodingName:nil baseURL:nil]; [self.view addSubview:webView]; }
这样就完成了,
这个demo主要就是练习。。有不对或者更好的方法,欢迎一起讨论。谢谢了。。。
源码看附件吧。。。
- WebService.zip (223.5 KB)
- 下载次数: 27
评论
打开工程一看不识别“NSURLConnectionDelegate,NSURLConnectionDataDelegate”协议,
估摸着不够新,有其它代替方式么
发表评论
-
IOS播放gif图
2012-09-10 11:10 12971-(void)gifImageView:(NSStrin ... -
UIScrollView循环滚动
2012-08-29 16:40 2091我弄了半天终于弄出UIScrollView循环了,分享一下,方 ... -
NSString与int和float的相互转换
2012-08-29 14:47 815NSString *tempA = @"123 ... -
iOS如何利用Delegate来实现两个UIView之间的传值
2012-08-23 13:50 1306转至:http://blog.csdn.net/whaomia ... -
切换两个UIVIew动画
2012-08-23 09:53 1143#pragma mark - Animation cod ... -
将UILabel旋转90度
2012-08-22 10:23 2891[*yourlabelname* setTransform:C ... -
UIAlertView的使用方法
2012-08-04 13:46 754UIAlertView类似于C#中的模态对话框 或 Me ... -
IOS隐藏键盘
2012-08-04 13:44 903//UITextField的Did End On Exi ...
相关推荐
IOS APP应用开发 天气预报开发 Json数据 详细代码 初学者应用
IOS应用源码——天气预报.zip
iOS 开发的简单代码。用josn获取API来实现简单的功能型的dome.适合初学者练手。
利用聚合数据提供的接口做的一个天气预报DEMO做练习。需要的朋友也可以学着做,仅供参考。
iOS选择不同城市的天气预报,可以显示出完善的天气预报,给大家做参考。
IOS实现天气预报。简单易懂。IOS的资源还是比较少一点吧。觉得例子不错。上传给大家学习。一起进步咯。
来源:Licence:Unspecified平台:iOS设备:iPhone作者:HuA_Lun 利用json解析返回天气信息。 Code4App编译测试,适用环境:Xcode 4.3, iOS 4.0以上。
Weather-App-React-Native:采用react-native构建的天气预报iOS App
很好用的 天气预报开发json解析数据接口端
天气预报 之DDClock 之滚动视图旋转菜单 状态栏-StatusBar Notifier View 状态栏之MPNotificationView Ad Controller AdMob demo Animation之Gmail Like Loading AWVersionAgent Bee Framework Circle Menu Cocoa ...
该应用程序专为天气预报而设计。
通过Swift学习iOS开发之一:天气预报APP 功能 创建 Single View Application with Swift 利用 CoreLocation 模块获取当前位置信息(经纬度) 安装库管理器 CocoaPods 通过 CoacoaPods 配置文件,安装网络通信库 ...
该源码可以实现一款非常简单的天气预报应用,该赢源码采用了json来进行解析,和asihttp请求数据作用,并且使用了中国天气网的天气预报的接口来实现整个应用的,是一个不错的ios天气预报应用开发学习的例子,喜欢的...
Android系统是Google公司开发的一个开源手机操作系统,它包括了操作系统、用户界面和应用程序,即智能手机工作所需的全部软件。Android的最大特点是它的开放性体系架构,不仅具有非常好的开发、调试环境,而且还具有...
该工程为iphone工程,基本功能为:通过PickerView选择省份和市名,然后返回该市当天以及未来6天的天气情况、最低最高温度。该工程重点为网络编程,如何根据链接使用NSURLSession获得数据,以及如何使用...
【项目资源】:包含前端、后端、移动开发、操作系统、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源、音视频、网站开发等各种技术项目的源码。包括STM32、ESP8266、PHP、QT、Linux、iOS、C++、...
【项目资源】:包含前端、后端、移动开发、操作系统、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源、音视频、网站开发等各种技术项目的源码。包括STM32、ESP8266、PHP、QT、Linux、iOS、C++、...