IOS-网络(发送JSON数据给服务器和多值参数)

本文介绍了一个iOS应用中如何通过POST请求发送JSON数据到服务器的示例代码。该示例展示了设置请求头、构建JSON数据体并发送请求的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

三步走:

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值