网络部分(iOS)
2015-11-03 09:42
246 查看
ftp:(文件传输协议)
http:(超文本传输协议)
https:(安全超文本传输协议)
file:(本地文件协议)
Xcode7设置网络:
打断点:在输出框 输入:po self.array 会打印出里面的对象
或者:
<key>NSAppTransportSecurity</key><dict>
<key>NSAllowsArbitraryLoads</key>
<true/></dict>
1> 获取网址
2> 建立连接,请求数据(NSMutableURLRequest *request),获取数据:NSURLConnection sendSynchronousRequest
3> 对数据进行解析
首先:[NSURLSession sharedSession]
设置URL
请求对象
建立连接 请求数据
有block
单独封装数据
设置body
建立连接 请求数据
http:(超文本传输协议)
https:(安全超文本传输协议)
file:(本地文件协议)
Xcode7设置网络:
打断点:在输出框 输入:po self.array 会打印出里面的对象
或者:
<key>NSAppTransportSecurity</key><dict>
<key>NSAllowsArbitraryLoads</key>
<true/></dict>
1. GET:
1.1 GET同步:
sendSynchronousRequest:request returningResponse:nil error:nil1> 获取网址
2> 建立连接,请求数据(NSMutableURLRequest *request),获取数据:NSURLConnection sendSynchronousRequest
3> 对数据进行解析
- (IBAction)getOne:(id)sender { // 初始化数组属性 self.arrayAllNews = [[NSMutableArray alloc] init]; // 1. 网址 NSString *string = @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213"; // url NSURL *url = [NSURL URLWithString:string]; // 2. 请求对象 可变的 传了地址(将url给一个请求对象) NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; // 3. 建立连接 请求数据 NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; // 4. 解析:在解析之前判断data是否拿到了数据 if (data != nil) { // 拿到最外层的字典 NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; // 用model处理数据 // 外层一个大的news 里面是一堆字典 for (NSDictionary *diction in dic[@"news"]) { NewsModel *model = [[NewsModel alloc] init]; [model setValuesForKeysWithDictionary:diction]; [self.arrayAllNews addObject:model]; } } // 遍历打印: for (NewsModel *model in self.arrayAllNews) { NSLog(@"%@", model); } }
1.2 GET session:
dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)首先:[NSURLSession sharedSession]
设置URL
请求对象
建立连接 请求数据
- (IBAction)session:(id)sender { NSURLSession *session = [NSURLSession sharedSession]; NSURL *url = [NSURL URLWithString:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { if (data) { NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; // 后面的内容:将字典转为model存储 和之前的一样 } }]; // 必须调用这个方法 才会有数据 [task resume]; }
1.3 GET 异步:
sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError)有block
- (IBAction)buttonGetTwo:(id)sender { self.arrayAllNews1 = [[NSMutableArray alloc] init]; NSURL *url = [NSURL URLWithString:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213"]; // 创建请求 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; // 默认就是GET形式 所以可以不用写 [request setHTTPMethod:@"GET"]; // 建立连接 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) { if (data != nil) { NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; for (NSDictionary *diction in dic[@"news"]) { NewsModel *model = [[NewsModel alloc] init]; [model setValuesForKeysWithDictionary:diction]; [self.arrayAllNews1 addObject:model]; } } for (NewsModel *model in self.arrayAllNews1) { NSLog(@"%@", model); } }]; }
2. POST:
2.1 POST异步:Block形式
设置URL单独封装数据
设置body
建立连接 请求数据
- (IBAction)postBlock:(id)sender { // 1. 初始化 self.arrayAllNews2 = [[NSMutableArray alloc] init]; // 2. 设置URL NSURL *url = [NSURL URLWithString:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; // 3. 制作body(将数据单独拿出来 封装) NSString *param = @"date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213"; NSData *data = [param dataUsingEncoding:NSUTF8StringEncoding]; // 4. 设置body [request setHTTPBody:data]; [request setHTTPMethod:@"POST"];// 这个要写 // connection建立连接 请求数据 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) { if (data) { NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; NSLog(@"%@",dic); for (NSDictionary *diction in dic[@"news"]) { NewsModel *model = [[NewsModel alloc] init]; [model setValuesForKeysWithDictionary:diction]; [self.arrayAllNews2 addObject:model]; } } for (NewsModel *model in self.arrayAllNews2) { NSLog(@"%@", model); } }]; }
2.2 POST代理:
遵守协议:<NSURLConnectionDataDelegate>#pragma mark POST异步代理形式 - (IBAction)postDelegate:(id)sender { NSURL *url = [NSURL URLWithString:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [request setHTTPMethod:@"POST"];// 设置请求形式 NSString *string = @"date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213"; NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding]; [request setHTTPBody:data]; // 建立连接 请求数据 NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; // 启动请求 才会调用代理方法 [connection start]; } #pragma mark POST代理形式的代理方法: #pragma 接收响应 -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { NSLog(@"接收到响应了"); self.data = [[NSMutableData alloc] init]; } #pragma 接收数据(拼接) -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [self.data appendData:data]; } #pragma 结束传输数据(然后进行解析) -(void)connectionDidFinishLoading:(NSURLConnection *)connection { // 数据解析:JSON // 返回的是id类型 (根据数据判断返回的具体类型) NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:self.data options:0 error:nil]; NSLog(@"%@",dic); }
相关文章推荐
- HTTP中在JAVAEE中最简单的应用
- RHCE 系列(二):如何进行包过滤、网络地址转换和设置内核运行时参数
- Android ImageView扩展 支持本地gif图片 支持网络gif图片
- HttpGet和HttpPost的区别
- RHCE 系列(二):如何进行包过滤、网络地址转换和设置内核运行时参数
- RHCE 系列(二):如何进行包过滤、网络地址转换和设置内核运行时参数
- http概述
- Win8无线网络受限或无线网络无法连接怎么办 Win8无线网络受限或无连接的五种解决方法
- iOS中 WGAFN_网络监控 技术分享
- 同步异步/阻塞非阻塞 和 5种linux网络通信模型
- windows上的5种网络通信模型示例代码
- 巧用Android网络通信技术,在网络上直接传输对象
- java网络编程——Socket和ServerSocket,套接字编程——简单聊天室改进
- 神经网络和机器学习基础入门分享
- NFS网络文件服务笔记之2
- NFS网络文件服务
- HTTP返回码总结
- iOS 9 网络请求
- HTTP错误代码详细介绍
- get 和post 的区别