IOS 开发 之 网络编程
2016-03-04 20:58
591 查看
如果此文帮助了您,请点击喜欢或评论,如果您喜欢我的文章请关注我,您的支持永远都是我前行的动力.
url : = 网络协议(http://今后可能是https) + 文件路径? + 参数1 & 参数2 …
2.接收响应
3.创建连接对象传输数据
xcode7 之后若想要使用http,需要修改info.plist文件
NSAppTransportSecucurity NSDictionary
NSAllowsArbitraryLoads BOOL YES;
2.创建请求对象
2.1 创建请求方式(默认是get,这一步可以不写)
3.创建响应对象(有时会出错)
4.创建连接对象
解析
开辟空间
1.创建url对象
2.创建请求对象
2.1(必须写) 创建请求方式
3.设置请求参数
4.创建响应对象
5.创建连接对象
6.解析
2.创建请求对象
3.创建连接对象
2.创建请求对象
2.1(必须写) 创建请求方式
3.设置请求参数
4.设置创建连接对象
2.创建请求对象
3.创建连接对象,并实现代理
4.开始执行
1.接收到服务器的响应
准备数据接收
2.接收请求的数据
将每次新接收到的数据拼接到原有数据包的后面;
3.加载完毕,开始解析
打印失败信息
1.接收到服务器的响应
准备数据接收
2.接收请求的数据
将每次新接收到的数据拼接到原有数据包的后面;
3.加载完毕,开始解析
打印失败信息
2.创建请求对象
2.1
参数转化
3.建立会话
session支持三种类型的任务
NSURLSessionDataTask;//(加载数据)
NSURLSessionDownloadTask;//(下载)
NSURLSessionUploadTask;//(上传)
//判断是否是子线程 1是子线程,0不是主线程
回到主线程(刷新数据)
相当于tableView的刷新数据
4.启动任务
2.创建session对象
3.创建task (该方法内部做了处理,默认使用get,直接传递Url即可);
转载请注明出处.
注意
1.发送请求url : = 网络协议(http://今后可能是https) + 文件路径? + 参数1 & 参数2 …
2.接收响应
3.创建连接对象传输数据
xcode7 之后若想要使用http,需要修改info.plist文件
NSAppTransportSecucurity NSDictionary
NSAllowsArbitraryLoads BOOL YES;
第一种get 同步
1.创建URL对象NSURL *url = [NSURL URLWithString:BASE_URL];
2.创建请求对象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
2.1 创建请求方式(默认是get,这一步可以不写)
[request setHTTPMethod:@"get"];
3.创建响应对象(有时会出错)
NSURLResponse *response = nil; NSError *error = nil;
4.创建连接对象
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
解析
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
开辟空间
self.dataArray = [NSMutableArray arrayWithCapacity:5]; for (NSDictionary *dict in dic[@"news"]) { News *news = [News new]; [news setValuesForKeysWithDictionary:dict]; [_dataArray addObject:news]; } for (News *news in _dataArray) { NSLog(@"%@",news); }
第二种 post 同步
注:浏览器只能模拟get1.创建url对象
NSURL *url = [NSURL URLWithString:URL_PORT1];
2.创建请求对象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
2.1(必须写) 创建请求方式
[request setHTTPMethod:@"post"];
3.设置请求参数
NSData *tempData = [URL_PORT2 dataUsingEncoding:NSUTF8StringEncoding]; [request setHTTPBody:tempData];
4.创建响应对象
NSURLResponse *response = nil; NSError *error = nil;
5.创建连接对象
NSData *data =[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
6.解析
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error]; NSLog(@"%@",dic); self.dataArray = [NSMutableArray arrayWithCapacity:5]; for (NSDictionary *dicq in dic[@"news"]) { News *news = [News new]; [news setValuesForKeysWithDictionary:dicq]; [_dataArray addObject:news]; } for (News *ee in _dataArray) { NSLog(@"%@",ee); }
第三种 GET异步 – BLOCK
创建URL对象NSURL *url = [NSURL URLWithString:BASE_URL];
2.创建请求对象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
3.创建连接对象
[NSOperationQueue mainQueue]主队列,多线程内容. __weak typeof (self)temp = self;
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue]completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil]; NSLog(@"get异步---%d---",[[NSThread currentThread]isMainThread]); temp.dataArray = [NSMutableArray arrayWithCapacity:5]; }]; NSLog(@"get异步---%d---",[[NSThread currentThread]isMainThread]); }
第四种 POST异步 – BLOCK
1.创建url对象NSURL *url = [NSURL URLWithString:URL_PORT1];
2.创建请求对象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
2.1(必须写) 创建请求方式
[request setHTTPMethod:@"post"];
3.设置请求参数
NSData *tempData = [URL_PORT2 dataUsingEncoding:NSUTF8StringEncoding]; [request setHTTPBody:tempData];
4.设置创建连接对象
__weak typeof (self)ttemp = self; [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) { if (nil != data) { NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil]; NSLog(@"%@",dic); } ttemp.dataArray = [NSMutableArray arrayWithCapacity:5]; }];
第五种 get异步 – 代理”
1.创建URL对象NSURL *url = [NSURL URLWithString:BASE_URL];
2.创建请求对象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
3.创建连接对象,并实现代理
NSURLConnection * connection = [NSURLConnection connectionWithRequest:request delegate:self];
4.开始执行
[connection start];
get异步代理
NSURLConnectionDataDelegate1.接收到服务器的响应
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
准备数据接收
self.tempData = [NSMutableData data]; }
2.接收请求的数据
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
将每次新接收到的数据拼接到原有数据包的后面;
[_tempData appendData:data]; }
3.加载完毕,开始解析
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{ NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:_tempData options:NSJSONReadingAllowFragments error:nil]; NSLog(@"%@",dic); }
打印失败信息
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ NSLog(@"文件链接出现了error:%@",error); }
第六种 post异步 – 代理
NSURL *url = [NSURL URLWithString:URL_PORT1];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"post"];
NSData *tempData = [URL_PORT2 dataUsingEncoding:NSUTF8StringEncoding]; [request setHTTPBody:tempData];
NSURLConnection *connertion = [NSURLConnection connectionWithRequest:request delegate:self];
[connertion start];
1.接收到服务器的响应
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
准备数据接收
self.tempData = [NSMutableData data]; }
2.接收请求的数据
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
将每次新接收到的数据拼接到原有数据包的后面;
[_tempData appendData:data]; }
3.加载完毕,开始解析
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{ NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:_tempData options:NSJSONReadingAllowFragments error:nil]; NSLog(@"%@",dic); }
打印失败信息
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ NSLog(@"文件链接出现了error:%@",error); }
post异步session
1.创建URL对象NSURL *url = [NSURL URLWithString:URL_PORT1];
2.创建请求对象
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
2.1
[request setHTTPMethod:@"post"];
参数转化
NSData *tempData = [URL_PORT2 dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:tempData];
3.建立会话
NSURLSession *session = [NSURLSession sharedSession];
session支持三种类型的任务
NSURLSessionDataTask;//(加载数据)
NSURLSessionDownloadTask;//(下载)
NSURLSessionUploadTask;//(上传)
//判断是否是子线程 1是子线程,0不是主线程
NSLog(@"%d",[[NSThread currentThread]isMainThread]); __weak typeof (self)temp = self; NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil]; //解析 NSLog(@"%@",dic); NSLog(@"---%d---",[[NSThread currentThread]isMainThread]);
回到主线程(刷新数据)
dispatch_async(dispatch_get_main_queue(), ^{
相当于tableView的刷新数据
[temp.tableView reloadData]; }); }];
4.启动任务
[task resume]; }
get异步session
1.创建url对象NSURL *url = [NSURL URLWithString:BASE_URL];
2.创建session对象
NSURLSession *session = [NSURLSession sharedSession];
3.创建task (该方法内部做了处理,默认使用get,直接传递Url即可);
NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
dispatch_async(dispatch_get_main_queue(), ^{ });
[task resume]; }
转载请注明出处.
相关文章推荐
- poj3469 Dual Core CPU 网络流 sap邻接表模板
- HttpClient4.5教程-序言
- ATS http — >https
- http://blog.sina.com.cn/s/blog_5b9b4abe01017638.html
- https通讯原理、加密算法、证书整理总结
- HTTPS科普扫盲帖
- 使用PC的adb工具通过wifi(TCP方式)调试安卓设备
- 找工作-——网络IO
- 获取网络gif时候报错 <Error>: ImageIO: _CGImagePluginInitGIF malformed GIF frame#0
- HTTP Live Streaming直播(iOS直播)技术分析与实现
- HTTP协议的学习
- OC-之AFNetworking与ASIHTTPRequest对比
- IHttpModule与IHttpHandler的区别整理
- Android操作HTTP实现与服务器通信
- 记录--java获取网络资源(图片、音频等)保存本地
- Python---TCP编程
- win7下面VM VirtualBox中linux CenOS 网络的配置
- 小笨狼漫谈多线程:GCD(一) http://www.cocoachina.com/ios/20160225/15422.html
- 网络爬虫的实现
- 10大常见的安全漏洞!你知道吗?