iOS_POST_网络请求

同步的 post 请求

#pragma mark - 同步的 post 请求
- (IBAction)POSTSynButtonDidClicked:(UIButton *)sender {

    // 1、拼接 baseUrlString
    NSString *baseUrlString = @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx";

    // 2、根据字符串创建 URL(统一资源定位符)
    NSURL *url = [NSURL URLWithString:baseUrlString];

    // 3、创建可变的 request 对象
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    // 1> 设置请求类型为 POST (默认为 GET)
    [request setHTTPMethod:@"POST"];

    // 2> 需要设置 POST 参数
    NSString *bodyString = @"date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213";
    // 把字符串转化为 NSData
    NSData *bodyData = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:bodyData];

    // 4、请求数据(同步)
    NSData *receiveData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

    // 5、系统自带json解析
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData: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);
    }
}

异步的 post 请求

#pragma mark - 异步的 post 请求
- (IBAction)POSTAsynButtonDidClicked:(UIButton *)sender {

    // 1、拼接 baseUrlString
    NSString *baseUrlString = @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx";

    // 2、根据字符串创建 url(统一资源定位符)
    NSURL *url = [NSURL URLWithString:baseUrlString];

    // 3、创建可变 request 对象
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    // 设置 request 对象的相关属性
    // 1>设置默认请求类型为 POST
    [request setHTTPMethod:@"POST"];

    // 2>设置POST参数
    NSString *bodyString = @"date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213";
    // 3>把字符串转化为 NSData
    NSData *bodyData = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:bodyData];

    // 4、请求数据(异步)
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

        // 5、系统自带json解析
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data 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);
        }

        // 对 tableView 进行 reloadData 否则第一次不显示内容
    }];


}

你可能感兴趣的:(网络,post,请求)