网络请求代理方式

#pragma mark-------网络请求代理方式

 

- (IBAction)DelegateButton:(UIButton *)sender {

    

    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";

    NSURL *url = [NSURL URLWithString:urlString];

    

    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    //请求数据代理回调的方式

    [NSURLConnection connectionWithRequest:request delegate:self];

}

 

//客户端收到服务器响应

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

{

    //初始化receiveData 对象

    self.receiveData = [NSMutableData data];

}

 

//收到数据

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

{

    //拼接数据

    [self.receiveData appendData:data];

}

 

//介绍数据完毕

- (void)connectionDidFinishLoading:(NSURLConnection *)connection

{

//    解析数据

    NSDictionary *dict =[NSJSONSerialization JSONObjectWithData:self.receiveData options:NSJSONReadingMutableContainers error:nil];

    NSLog(@"%@", dict);

    

}

 

//网络请求失败的时候

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

{

    NSLog(@"%@", error);

}

 

posted @ 2015-07-21 16:45  程序猿大鹏  阅读(151)  评论(0编辑  收藏  举报