<code class = "hljs" objectivec= "" >#pragma mark - 异步的 get 请求
- (IBAction)GETAsyButtonDidClicked:(UIButton *)sender {
// 1、拼接 urlString,网址里面必须写 http://
NSString *urlString = @http : //ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213;
// 2、根据字符串创建 URL(统一资源定位符)
NSURL *url = [NSURL URLWithString:urlString];
// 3、根据 url 创建 request 请求类的对象
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 4、开始去请求网络、数据(同步) 返回data
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
// 5、系统自带json解析
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
NSArray *array = dict[ @news ];
self.newsArray = [NSMutableArray array];
for (NSDictionary *smallDict in array) {
NewsModal *modal = [[NewsModal alloc] init];
[modal setValuesForKeysWithDictionary:smallDict];
[self.newsArray addObject:modal];
}
for (NewsModal *modal in self.newsArray) {
NSLog(@%@, modal.title);
}
}];
}
</code>
|