iOS多线程与网络开发之使用POST上传JSON数据 & 多值参数

来源:互联网 发布:喷绘用什么软件做 编辑:程序博客网 时间:2024/04/28 08:20

郝萌主倾心贡献,尊重作者的劳动成果,请勿转载。

如果文章对您有所帮助,欢迎给作者捐赠,支持郝萌主,捐赠数额随意,重在心意^_^ 

我要捐赠: 点击捐赠

Cocos2d-X源码下载:点我传送

游戏官方下载:http://dwz.cn/RwTjl

游戏视频预览:http://dwz.cn/RzHHd

游戏开发博客:http://dwz.cn/RzJzI

游戏源码传送http://dwz.cn/Nret1


A.上传JSON
1.思路:
必须使用POST方法才能上传大量JSON数据
设置请求头:设置Content-Type
设置请求体,JSON实际相当于字典,可以用NSDictionary
NSJSONSerialization把字典数据转换成JSON二进制
 
 
2.实现
复制代码
 1 // 2 //  ViewController.m 3 //  PostJsonDemo 4 // 5 //  Created by haomengzhu on 15/1/28. 6 //  Copyright (c) 2015年 haomengzhu. All rights reserved. 7 // 8  9 #import "ViewController.h"10 11 @interface ViewController ()12 - (IBAction)postJson;13 14 @end15 16 @implementation ViewController17 18 - (void)viewDidLoad {19     [super viewDidLoad];20     // Do any additional setup after loading the view, typically from a nib.21 }22 23 - (IBAction)postJson {24     // 1.创建请求25     NSURL *url = [NSURL URLWithString:@"http://192.168.0.21:8080/MyTestServer/acceptJson"];26     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];27     // 设置post发送28     request.HTTPMethod = @"POST";29    30     // 2.设置请求头31     [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];32    33     // 3.设置请求体34     NSDictionary *json = @{@"name":@"tom",35                            @"age":@"21"};36     request.HTTPBody = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:nil];37    38    39     // 4.发送请求40     [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {41         NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]);42     }];43    44 }45 @end
复制代码
 
 
B.多值参数
1.概念
一个参数名对应多个参数值
http://localhost:8080/MyTestServer/upload?type=aaa&type=bbb&type=ccc
这样在服务器接收到的就是一个数组
 

郝萌主倾心贡献,尊重作者的劳动成果,请勿转载。

如果文章对您有所帮助,欢迎给作者捐赠,支持郝萌主,捐赠数额随意,重在心意^_^ 

我要捐赠: 点击捐赠

Cocos2d-X源码下载:点我传送

游戏官方下载:http://dwz.cn/RwTjl

游戏视频预览:http://dwz.cn/RzHHd

游戏开发博客:http://dwz.cn/RzJzI

游戏源码传送http://dwz.cn/Nret1

1 0
原创粉丝点击