网络编程_post_异步-请求_获取网络数据

    //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);

        }

        

        

        

    }];

你可能感兴趣的:(iOS,UI)