iOS网络编程:五、Post代理异步

来源:互联网 发布:只有程序员才懂的笑话 编辑:程序博客网 时间:2024/05/18 01:14
@interface RootViewController ()<NSURLConnectionDataDelegate>@property (nonatomic, strong) NSMutableArray *data;@property (nonatomic, strong) NSMutableData *tempData;@end

注:在Button的响应之下

- (void)PostdelegateAsyncAction{    NSURL *url = [NSURL URLWithString:ZQPOSTURL];    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];    [request setHTTPMethod:@"POST"];    NSData *data = [ZQPOSTBODY dataUsingEncoding:NSUTF8StringEncoding];    [request setHTTPBody:data];    [NSURLConnection connectionWithRequest:request delegate:self];}- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{    self.data = [NSMutableArray array];    self.tempData = [NSMutableData data];}- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{    [self.tempData appendData:data];}- (void)connectionDidFinishLoading:(NSURLConnection *)connection{    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:self.tempData options:NSJSONReadingAllowFragments error:nil];    NSArray *arr = dic[@"news"];    for (NSDictionary *dic in arr) {        News *news = [[News alloc] init];        [news setValuesForKeysWithDictionary:dic];        [self.data addObject:news];    }}
0 0
原创粉丝点击