网络编程_post_异步-请求_获取网络数据
2015-09-28 18:59
531 查看
//1.获取URL
NSURL *url = [[NSURL
alloc]initWithString:POST_URL];
//2.创建请求对象
NSMutableURLRequest *mutableRequest = [[NSMutableURLRequest
alloc]initWithURL:url];
[mutableRequest
setHTTPMethod:@"POST"];
NSData *bodyData = [POST_BODY
dataUsingEncoding:NSUTF8StringEncoding];
[mutableRequest
setHTTPBody:bodyData];
//3.创建链接
[NSURLConnection
sendAsynchronousRequest:mutableRequest
queue:[NSOperationQueue
mainQueue] completionHandler:^(NSURLResponse *response,
NSData *data, NSError *connectionError) {
//解析数据
NSDictionary *dictionary = [NSJSONSerialization
JSONObjectWithData:data options:NSJSONReadingAllowFragments
error:nil];
_dataArray = [[NSMutableArray
alloc]initWithCapacity:5];
for (NSDictionary *dict
in [dictionary objectForKey:@"news"]) {
News *news = [News
new];
[news setValuesForKeysWithDictionary:dict];
[_dataArray
addObject:news];
}
for (News *news
in _dataArray) {
NSLog(@"%@",news);
}
}];
NSURL *url = [[NSURL
alloc]initWithString:POST_URL];
//2.创建请求对象
NSMutableURLRequest *mutableRequest = [[NSMutableURLRequest
alloc]initWithURL:url];
[mutableRequest
setHTTPMethod:@"POST"];
NSData *bodyData = [POST_BODY
dataUsingEncoding:NSUTF8StringEncoding];
[mutableRequest
setHTTPBody:bodyData];
//3.创建链接
[NSURLConnection
sendAsynchronousRequest:mutableRequest
queue:[NSOperationQueue
mainQueue] completionHandler:^(NSURLResponse *response,
NSData *data, NSError *connectionError) {
//解析数据
NSDictionary *dictionary = [NSJSONSerialization
JSONObjectWithData:data options:NSJSONReadingAllowFragments
error:nil];
_dataArray = [[NSMutableArray
alloc]initWithCapacity:5];
for (NSDictionary *dict
in [dictionary objectForKey:@"news"]) {
News *news = [News
new];
[news setValuesForKeysWithDictionary:dict];
[_dataArray
addObject:news];
}
for (News *news
in _dataArray) {
NSLog(@"%@",news);
}
}];
相关文章推荐
- 网络编程_post_异步-代理_获取网络数据
- HttpClient Demo
- 网络编程_post_同步_获取网络数据
- 网络编程_get_异步-请求_获取网络数据
- iOS开发 调试 网络限速
- AngularJS 用 $http.jsonp 方法跨域请求数据错误的问题
- 网络访问请求关于封装了拼接url的方法。
- 网络图片加载
- Angular JS 学习笔记(自定义服务:factory,Promise 模式异步请求查询:$http,过滤器用法filter,指令:directive)
- windows网络联接问题
- 网络编程_get_异步-代理_获取网络数据
- 网络编程_get_同步_获取网络数据
- HTTP协议详解(真的很经典)
- 移动虚拟机后修改 MAC 地址
- 河南第三届ACM省赛(网络的可靠性)
- HttpClient详解一
- 【Android应用开发技术:网络通信】Socket套接字
- 网络工程师必备知识:苹果MAC系统下使用USB转console线配置交换机的连接方法
- HDU 5493 Queue (线段树) 2015合肥网络赛
- https协议