标签:line action dcl finish sar mutable lis 字符串 tar
#pragma mark - 网络请求代理方式(异步)
- (IBAction)DelegateButtonDidClicked:(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、请求数据(代理回调方式)遵守代理 <NSURLConnectionDataDelegate>
[NSURLConnection connectionWithRequest:request delegate:self];
}
代理方法
client收到server的响应
#pragma mark | client收到server的响应
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
// 仅仅运行一次
// 初始化 receiveData 对象
self.receiveData = [NSMutableData data];
}
client收数据(并不是一次性所有拿到)
#pragma mark | client收数据(并不是一次性所有拿到)
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// 每次返回的都是新数据
// 拼接数据(请求一点 拼接一点)
[self.receiveData appendData:data];
}
client接收数据完成
#pragma mark | client接收数据完成
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// 解析数据
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:self.receiveData 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);
}
}
网络请求失败
#pragma mark | 网络请求失败
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"%@", error);
}
标签:line action dcl finish sar mutable lis 字符串 tar
原文地址:http://www.cnblogs.com/cynchanpin/p/7109374.html