您的位置:首页 > 移动开发 > IOS开发

iOS 给服务器发送Json数据demo

2015-10-28 00:00 260 查看
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//1.创建请求

NSURL *url=[NSURL URLWithString:@"http://192.168.1.121:8090/"];
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
request.HTTPMethod=@"POST";
//2.设置请求头
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
//3.设置请求体
NSDictionary *dict=@{@"name":@"chen",@"sex":@"nan",@"shop":@"tool"};

//NSData --->NSDictionary
//NSDictionary-->>NSData
NSData *data=[NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
request.HTTPBody=data;
//发送请求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSLog(@"%ld",data.length);
}];

}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Json数据 服务器