发送json数据给服务器
来源:互联网 发布:js异或运算符 编辑:程序博客网 时间:2024/05/17 06:07
发送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
0 0
- 给服务器发送JSON数据
- 发送JSON数据给服务器
- 给服务器发送JSON数据
- 发送json数据给服务器
- iOS开发-给服务器发送JSON数据
- swift3.0 发送json数据给服务器
- Android给服务器发送json数据初体验
- 发送json数据给服务器以及多值参数
- 用AFN发送字符串形式的Json数据给服务器
- 发送json字符串给服务器
- 发送xml数据给服务器
- 发送JSON数据到服务器
- iOS开发网络篇—发送json数据给服务器以及多值参数
- iOS开发网络篇—发送json数据给服务器以及多值参数
- iOS开发网络—11发送json数据给服务器以及多值参数
- iOS开发网络篇—发送json数据给服务器以及多值参数
- iOS开发网络篇—发送json数据给服务器以及多值参数
- iOS开发网络篇—发送json数据给服务器以及多值参数
- 同样是公务员,为什么有人年薪4万有人40万?
- matrikon_opc模拟器使用
- Android 学习笔记 Contacts (一)ContentResolver query 参数详解
- IntelliJ IDEA 中把本地的工程保存到本地的 git 仓库的方法和把本地的工程发布到 github 的方法
- spring中的BeanFactory与ApplicationContext的作用和区别?
- 发送json数据给服务器
- JS打印等腰三角形
- LayoutInflater详解
- ACM--三角形重心--HDOJ 2105--The Center of Gravity
- C# ThreadPool的简单用法
- 三次握手
- Pyton插件安装到eclipse后报Project interpreter not specified这个问题
- semaphore
- 1.Beyond Compare 相同文件对比结果仍显示红色 解决方案