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

来源:互联网 发布:淘宝店怎么改名字 编辑:程序博客网 时间:2024/05/17 22:30

    //1.获取URL

   NSURL *url = [[NSURLalloc]initWithString:POST_URL];

    

    //2.创建请求对象

    NSMutableURLRequest *mutableRequest = [[NSMutableURLRequestalloc]initWithURL:url];

    

    [mutableRequestsetHTTPMethod:@"POST"];

    

    NSData *bodyData = [POST_BODYdataUsingEncoding:NSUTF8StringEncoding];

    

    [mutableRequestsetHTTPBody:bodyData];

    

    //3.创建链接

    [NSURLConnectionsendAsynchronousRequest:mutableRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response,NSData *data, NSError *connectionError) {

        

       //解析数据

        NSDictionary *dictionary = [NSJSONSerializationJSONObjectWithData:data options:NSJSONReadingAllowFragmentserror:nil];

        _dataArray = [[NSMutableArrayalloc]initWithCapacity:5];

        

       for (NSDictionary *dictin [dictionary objectForKey:@"news"]) {

           News *news = [Newsnew];

            [news setValuesForKeysWithDictionary:dict];

            [_dataArrayaddObject:news];

        }

       for (News *newsin _dataArray) {

           NSLog(@"%@",news);

        }

        

        

        

    }];

0 0
原创粉丝点击