iOS开发网络篇—发送json数据给服务器以及多值参数
2016-06-06 09:13
513 查看
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数据为请求体
代码示例:
#import "YYViewController.h" @interface YYViewController () @end @implementation YYViewController - (void)viewDidLoad { [super viewDidLoad]; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // 1.创建请求 NSURL *url = [NSURL URLWithString:@"http://192.168.1.200:8080/MJServer/order"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; request.HTTPMethod = @"POST"; // 2.设置请求头 [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; // 3.设置请求体 NSDictionary *json = @{ @"order_id" : @"123", @"user_id" : @"789", @"shop" : @"Toll" }; // NSData --> NSDictionary // NSDictionary --> NSData NSData *data = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:nil]; request.HTTPBody = data; // 4.发送请求 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { NSLog(@"%d", data.length); }]; } @end
二、多值参数
多值参数:一个参数对应多个值。
如下面的请求参数:
http://192.168.1.103:8080/MJServer/weather?place=北京&place=河南&place=湖南
服务器的place属性是一个数组。因此用同一个参数不会把服务器的值覆盖。
相关文章推荐
- iOS开发网络篇—监测网络状态
- iOS开发网络篇—简单介绍ASI框架的使用
- iOS开发网络篇—文件的上传
- iOS开发网络篇—多线程断点下载
- HTTP请求流程介绍
- httpd之防盗链和限制下载
- httpd之静态缓存
- 网络demo
- httpd之使用rewrite对目录进行访问控制
- Android官方开发文档Training系列课程中文版:连接无线设备之网络服务搜索功能
- Volley网络请求框架
- http协议详解
- TCP 的那些事儿系列
- OkHttp3浅析
- 知识付费还是内容付费?网络时代的内容、体验与注意力
- Ceph心跳与网络
- IDC行业往何处去?
- Java 网络IO编程总结(BIO、NIO、AIO均含完整实例代码)
- Linux 网络 I/O 模型简介(图文)
- IDC行业往何处去?