您的位置:首页 > 理论基础 > 计算机网络

网络编程_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);
}

}];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: