[iOS 多线程 & 网络 - 2.6] - 使用POST上传JSON数据 & 多值参数
2015-01-29 00:30
483 查看
A.上传JSON
1.思路:
必须使用POST方法才能上传大量JSON数据
设置请求头:设置Content-Type
设置请求体,JSON实际相当于字典,可以用NSDictionary
NSJSONSerialization把字典数据转换成JSON二进制
2.实现
B.多值参数
1.概念
一个参数名对应多个参数值 http://localhost:8080/MyTestServer/upload?type=aaa&type=bbb&type=ccc 这样在服务器接收到的就是一个数组
1.思路:
必须使用POST方法才能上传大量JSON数据
设置请求头:设置Content-Type
设置请求体,JSON实际相当于字典,可以用NSDictionary
NSJSONSerialization把字典数据转换成JSON二进制
2.实现
// // ViewController.m // PostJsonDemo // // Created by hellovoidworld on 15/1/28. // Copyright (c) 2015年 hellovoidworld. All rights reserved. // #import "ViewController.h" @interface ViewController () - (IBAction)postJson; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (IBAction)postJson { // 1.创建请求 NSURL *url = [NSURL URLWithString:@"http://192.168.0.21:8080/MyTestServer/acceptJson"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; // 设置post发送 request.HTTPMethod = @"POST"; // 2.设置请求头 [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; // 3.设置请求体 NSDictionary *json = @{@"name":@"tom", @"age":@"21"}; request.HTTPBody = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:nil]; // 4.发送请求 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]); }]; } @end
B.多值参数
1.概念
一个参数名对应多个参数值 http://localhost:8080/MyTestServer/upload?type=aaa&type=bbb&type=ccc 这样在服务器接收到的就是一个数组
相关文章推荐
- iOS多线程与网络开发之使用POST上传JSON数据 & 多值参数
- [iOS 多线程 & 网络 - 2.11] - ASI框架上传文件
- Volley 重写源码,使用Post方式获取网络Json数据
- FireFox插件RESTClient 如何使用POST,请求json数据参数。
- iOS网络开发——JSON数据解码(使用NSJSONSerialization)
- iOS网络编程4--使用SwiftyJSON解析JSON数据
- [iOS 多线程 & 网络 - 4.0] - AFN框架简单使用
- iOS 多线程 & 网络 - 4.0 - AFN框架简单使用
- IOS-网络(发送JSON数据给服务器和多值参数)
- iOS—网络实用技术OC篇&网络爬虫-使用java语言抓取网络数据
- iOS开发——网络实用技术OC篇&网络爬虫-使用青花瓷抓取网络数据
- [iOS 多线程 & 网络 - 4.0] - AFN框架简单使用
- [iOS 多线程 & 网络 - 2.1] - 解析json
- Android post请求网络数据(json作为参数)
- iOS开发使用JSON解析网络数据
- iOS 如何用post方式上传json数据
- iOS-使用NSURLConnection进行Http Get和Post请求网络数据
- IOS中网络数据请求过程详解(GET POST方法使用)
- iOS网络篇—发送json数据给服务器以及多值参数
- IOS学习之 网络编程(8)--发送json数据给服务器以及多值参数