三步走:
1.使用POST请求
2.设置请求头
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
3.设置JSON数据为请求体
1 // 2 // ViewController.m 3 // IOS_0130_发送JSON给服务器 4 // 5 // Created by ma c on 16/1/30. 6 // Copyright © 2016年 博文科技. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 #import "MBProgressHUD+MJ.h" 11 12 @interface ViewController () 13 14 @end 15 16 @implementation ViewController 17 18 - (void)viewDidLoad { 19 [super viewDidLoad]; 20 self.view.backgroundColor = [UIColor cyanColor]; 21 // Do any additional setup after loading the view, typically from a nib. 22 } 23 24 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event 25 { 26 //发送JSON数据给服务器 27 //[self sendJSON]; 28 //多值参数 29 [self multiValueParameter]; 30 31 } 32 ///多值参数 33 - (void)multiValueParameter 34 { 35 //1.URL 36 NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/weather"]; 37 //2.请求 38 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 39 //3.请求方法 40 request.HTTPMethod = @"POST"; 41 //4.设置请求体 42 NSMutableString *param = [NSMutableString string]; 43 [param appendString:@"place=BeiJing&place=TianJin&place=AnHui"]; 44 // [param appendString:@"&place=TianJin"]; 45 // [param appendString:@"&place=AnHui"]; 46 request.HTTPBody = [param dataUsingEncoding:NSUTF8StringEncoding]; 47 48 //5.发送请求 49 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) { 50 if (data == nil || connectionError) return; 51 NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]; 52 NSString *error = dict[@"error"]; 53 if (error) { 54 [MBProgressHUD showError:error]; 55 } 56 else{ 57 NSArray *weathers = dict[@"weathers"]; 58 NSLog(@"%@",weathers); 59 } 60 }]; 61 } 62 ///发送JSON数据给服务器 63 - (void)sendJSON 64 { 65 //1.URL 66 NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/order"]; 67 //2.请求 68 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 69 //3.请求方法 70 request.HTTPMethod = @"POST"; 71 //4.设置请求体 72 //创建一个描述订单信息的JSON数据 73 NSDictionary *dict = @{ 74 @"shop_id" : @"123456", 75 @"shop_name" : @"bowen", 76 @"user_id" : @"2012110606" 77 }; 78 NSData *json = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil]; 79 request.HTTPBody = json; 80 81 //5.设置请求头:这次请求体的数据不再是参数,而是JSON数据 value:MIMEType 82 [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 83 84 //6.发送请求 85 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) { 86 if (data == nil || connectionError) return; 87 NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]; 88 NSString *error = dict[@"error"]; 89 if (error) { 90 [MBProgressHUD showError:error]; 91 } 92 else{ 93 NSString *success = dict[@"success"]; 94 [MBProgressHUD showSuccess:success]; 95 } 96 }]; 97 } 98 @end