[iOS 多线程 & 网络 - 2.6] - 使用POST上传JSON数据 & 多值参数
1 // 2 // ViewController.m 3 // PostJsonDemo 4 // 5 // Created by hellovoidworld on 15/1/28. 6 // Copyright (c) 2015年 hellovoidworld. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 11 @interface ViewController () 12 - (IBAction)postJson; 13 14 @end 15 16 @implementation ViewController 17 18 - (void)viewDidLoad { 19 [super viewDidLoad]; 20 // Do any additional setup after loading the view, typically from a nib. 21 } 22 23 - (IBAction)postJson { 24 // 1.创建请求 25 NSURL *url = [NSURL URLWithString:@"http://192.168.0.21:8080/MyTestServer/acceptJson"]; 26 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 27 // 设置post发送 28 request.HTTPMethod = @"POST"; 29 30 // 2.设置请求头 31 [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 32 33 // 3.设置请求体 34 NSDictionary *json = @{@"name":@"tom", 35 @"age":@"21"}; 36 request.HTTPBody = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:nil]; 37 38 39 // 4.发送请求 40 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { 41 NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]); 42 }]; 43 44 } 45 @end
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。