IOS开发之在服务器端获取数据,保存网页的Demo学习
2015-01-29 20:46
417 查看
新建一个SingleViewApplication应用, 在storyboard中拖2个label,然后创建他们的弱链接,得到如下的代码:
.m文件的整体变量申明代码如下:
在viewDidLoad中添加以下代码:
注意:在第二个label显示获取信息的时候,将显示行数修改,并将label拉高拉宽,否则显示不全,如图:右边属性中的Line改为5;
v
好了,现在运行即可。结果:
@property (weak, nonatomic) IBOutlet UILabel *label; @property (weak, nonatomic) IBOutlet UILabel *labelResult;
.m文件的整体变量申明代码如下:
#import "ViewController.h"
@interface ViewController ()
@property (retain, nonatomic) NSURLConnection * connection;
@property (retain, nonatomic) NSMutableData * data;
@property (weak, nonatomic) IBOutlet UILabel *label; @property (weak, nonatomic) IBOutlet UILabel *labelResult;
@end
在viewDidLoad中添加以下代码:
@implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.label.text = @"正在请求数据"; //访问服务器获取json数据 NSString *urlSring = @"http://www.weather.com.cn/data/cityinfo/101020100.html"; NSURL *url = [NSURL URLWithString:urlSring]; //step2:实例化一个request NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0]; //创建链接 self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; if (self.connection) { NSLog(@"创建成功"); }else{ NSLog(@"创建失败");//(@"创建失败“); } }添加如下函数:
-(void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *)response{ //接受一个服务端回话,再次一般初始化接受数据的对象 NSMutableData *data = [NSMutableData alloc]; self.data = data; NSLog(@"接受服务端回话"); } -(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ //接受返回数据,这个方法可能会被调用多次,因此将多次返回数据加起来 NSInteger datalength = [data length]; NSLog(@"返回数据量: %ld", (long)datalength); [self.data appendData:data]; } -(NSString *)dataFilePath:(NSString *)fileName{ NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES); NSString *document = [path objectAtIndex:0]; NSLog(@"path: %@",path); return [document stringByAppendingPathComponent:fileName]; } -(void) connectionDidFinishLoading:(NSURLConnection *)connection{ //NSLog(@"%d", [self.datalength]); self.label.text = @"请求结束"; NSString *mystr = [[NSString alloc]initWithData:self.data encoding:NSUTF8StringEncoding]; [mystr writeToFile:[self dataFilePath:@"百度图片-全球最大中文图片库.html"] atomically:YES encoding:NSUTF8StringEncoding error:nil]; NSLog(@"最后的结果%@", mystr); self.label.text = @"最后的结果"; self.labelResult.text = mystr; // NSDictionary *weather = [NSJSONSerialization JSONObjectWithData:self.data options:NSJSONReadingMutableContainers error:nil]; // NSLog(@"%@",weather); // [weather writeToFile:[self dataFilePath:@"weather.plist"] atomically:YES]; // NSLog(@"%@",weather); } -(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ self.label.text = @"链接失败"; }
注意:在第二个label显示获取信息的时候,将显示行数修改,并将label拉高拉宽,否则显示不全,如图:右边属性中的Line改为5;
v
好了,现在运行即可。结果:
相关文章推荐
- IOS开发---菜鸟学习之路--(十三)-利用MBProgressHUD进行异步获取数据
- IOS开发---菜鸟学习之路--(二)-数据获取
- android开发学习笔记(一)分别通过GET和POST请求从服务器端获取数据
- IOS开发---菜鸟学习之路--(十二)-利用ASIHTTPRequest进行异步获取数据
- iOS项目开发实战——获取网页源代码的二进制数据
- IOS开发---菜鸟学习之路--(二)-数据获取
- IOS开发学习笔记(八)——IOS开发之数据保存
- 【零基础学习iOS开发】【02-C语言】07-基本数据类型
- 网站学习备忘001——运用POST方法获取表单中的数据,并显示在同一网页中
- IOS开发之Post 方式获取服务器数据
- Extjs学习------MVC结构的开发(从后台获取数据显示到Grid)
- [IOS]UIWebView实现保存页面和读取服务器端json数据
- SilverLight商业应用程序开发---学习笔记(6)从服务器中获取数据之二
- IOS学习九:ios开发之数据的持久化存储机制
- IOS开发获取webView中网页的信息
- ios开发,将子线程获取的数据实时更新到UI
- [IOS]UIWebView实现保存页面和读取服务器端json数据
- IOS开发基础教程学习笔记2 DatePicker获取时间
- IOS 获取UIWebView加载网页的上的数据实例
- Windows Phone 开发学习笔记(九) RSS阅读器之获取数据