iOS开发网络篇—发送json数据给服务器以及多值参数
2015-12-20 15:03
701 查看
iOS开发网络篇—发送json数据给服务器以及多值参数
一、发送JSON数据给服务器
发送JSON数据给服务器的步骤:
(1)一定要使用POST请求
(2)设置请求头
(3)设置JSON数据为请求体
代码示例:
二、多值参数
多值参数:一个参数对应多个值。
如下面的请求参数:
http://192.168.1.103:8080/MJServer/weather?place=北京&place=河南&place=湖南
服务器的place属性是一个数组。因此用同一个参数不会把服务器的值覆盖。
一、发送JSON数据给服务器
发送JSON数据给服务器的步骤:
(1)一定要使用POST请求
(2)设置请求头
(3)设置JSON数据为请求体
代码示例:
1 #import "YYViewController.h" 2 3 @interface YYViewController () 4 5 @end 6 7 @implementation YYViewController 8 9 - (void)viewDidLoad 10 { 11 [super viewDidLoad]; 12 } 13 14 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 15 { 16 // 1.创建请求 17 NSURL *url = [NSURL URLWithString:@"http://192.168.1.200:8080/MJServer/order"]; 18 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 19 request.HTTPMethod = @"POST"; 20 21 // 2.设置请求头 22 [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 23 24 // 3.设置请求体 25 NSDictionary *json = @{ 26 @"order_id" : @"123", 27 @"user_id" : @"789", 28 @"shop" : @"Toll" 29 }; 30 31 // NSData --> NSDictionary 32 // NSDictionary --> NSData 33 NSData *data = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:nil]; 34 request.HTTPBody = data; 35 36 // 4.发送请求 37 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { 38 NSLog(@"%d", data.length); 39 }]; 40 } 41 42 @end
二、多值参数
多值参数:一个参数对应多个值。
如下面的请求参数:
http://192.168.1.103:8080/MJServer/weather?place=北京&place=河南&place=湖南
服务器的place属性是一个数组。因此用同一个参数不会把服务器的值覆盖。
相关文章推荐
- iOS开发网络篇—文件的上传
- iOS开发网络篇—多线程断点下载
- iOS开发网络篇—数据安全
- Python 网络爬虫单线程版
- android网络编程之HttpUrlConnection的讲解--上传大文件
- tomcat搭建https
- OSGI系列 Servlet-Rest发布Http服务
- IPV6网络优化脚本.bat
- android 用闹钟定时做http请求推送解决方案
- 【socket】socket介绍-socket与http服务通信
- tcpdump抓SQL
- Ejabberd网络结构
- 《深入浅出node.js》笔记-node构建http服务
- 为什么 UDP 有时比 TCP 更有优势
- [CAQ5]使用okhttp等三方网络框架的时候出现超时错误
- 减号的优先级http://www.nowcoder.com/profile/380772/test/2198787/873#summary
- std::vector::iterator重载了下面哪些运算符 http://www.nowcoder.com/profile/380772/test/2198787/1141?onlyWrong=0
- 云计算应用网络平台按内蒙古区域和行业划分来创建和管理
- 一张图说明CDN网络的原理
- 重载运算符 http://www.nowcoder.com/profile/380772/test/2198787/2593?onlyWrong=0