客户端封装数据为json格式的数据提交到服务端的方法
2014-08-21 11:32
531 查看
使用jsonkit封装字典为json数据
/************第一种方法:使用jsonkit封装字典为json数据************/
NSDictionary *dataDictionary=[NSDictionary dictionaryWithObjectsAndKeys:
@"100110004777",@"no",
@"123321",@"pwd",
@"1111",@"check_code",
nil];
NSString *tempJsonData=[dataDictionary JSONString];
NSData *jsonData = [tempJsonData dataUsingEncoding:NSUTF8StringEncoding];
NSMutableData *JsonDataOK= [NSMutableData dataWithData:jsonData];
NSURL *url1 = [NSURL URLWithString:@"http://172.17.49.200:8341/app/100001.ha"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url1];
[request addRequestHeader:@"Content-Type" value:@"application/json; encoding=utf-8"];
[request addRequestHeader:@"Accept" value:@"application/json"];
[request setRequestMethod:@"POST"];
[request setPostBody:JsonDataOK];
[request setDelegate:self];
[request startSynchronous];
/************第二种方法:使用原生方法封装字典为json数据************/
// NSDictionary *user = [[NSDictionary alloc] initWithObjectsAndKeys:
// @"100110004777",@"no",
// @"123321",@"pwd",
// @"4620",@"check_code",
// nil];
// if ([NSJSONSerialization isValidJSONObject:user])
// {
// NSError *error;
// NSData *jsonData = [NSJSONSerialization dataWithJSONObject:user options:NSJSONWritingPrettyPrinted error: &error];
// NSMutableData *tempJsonData = [NSMutableData dataWithData:jsonData];
// //NSLog(@"Register JSON:%@",[[NSString alloc] initWithData:tempJsonData encoding:NSUTF8StringEncoding]);
//
// NSURL *url = [NSURL URLWithString:@"http://172.17.49.200:8341/app/100001.ha"];
// ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
// [request addRequestHeader:@"Content-Type" value:@"application/json; encoding=utf-8"];
// [request addRequestHeader:@"Accept" value:@"application/json"];
// [request setRequestMethod:@"POST"];
// [request setDelegate:self];
// [request setPostBody:tempJsonData];
// [request startSynchronous];
// NSError *error1 = [request error];
// if (!error1) {
// NSString *response = [request responseString];
// NSLog(@"Test:%@",response);
// }
// }
/************第一种方法:使用jsonkit封装字典为json数据************/
NSDictionary *dataDictionary=[NSDictionary dictionaryWithObjectsAndKeys:
@"100110004777",@"no",
@"123321",@"pwd",
@"1111",@"check_code",
nil];
NSString *tempJsonData=[dataDictionary JSONString];
NSData *jsonData = [tempJsonData dataUsingEncoding:NSUTF8StringEncoding];
NSMutableData *JsonDataOK= [NSMutableData dataWithData:jsonData];
NSURL *url1 = [NSURL URLWithString:@"http://172.17.49.200:8341/app/100001.ha"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url1];
[request addRequestHeader:@"Content-Type" value:@"application/json; encoding=utf-8"];
[request addRequestHeader:@"Accept" value:@"application/json"];
[request setRequestMethod:@"POST"];
[request setPostBody:JsonDataOK];
[request setDelegate:self];
[request startSynchronous];
/************第二种方法:使用原生方法封装字典为json数据************/
// NSDictionary *user = [[NSDictionary alloc] initWithObjectsAndKeys:
// @"100110004777",@"no",
// @"123321",@"pwd",
// @"4620",@"check_code",
// nil];
// if ([NSJSONSerialization isValidJSONObject:user])
// {
// NSError *error;
// NSData *jsonData = [NSJSONSerialization dataWithJSONObject:user options:NSJSONWritingPrettyPrinted error: &error];
// NSMutableData *tempJsonData = [NSMutableData dataWithData:jsonData];
// //NSLog(@"Register JSON:%@",[[NSString alloc] initWithData:tempJsonData encoding:NSUTF8StringEncoding]);
//
// NSURL *url = [NSURL URLWithString:@"http://172.17.49.200:8341/app/100001.ha"];
// ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
// [request addRequestHeader:@"Content-Type" value:@"application/json; encoding=utf-8"];
// [request addRequestHeader:@"Accept" value:@"application/json"];
// [request setRequestMethod:@"POST"];
// [request setDelegate:self];
// [request setPostBody:tempJsonData];
// [request startSynchronous];
// NSError *error1 = [request error];
// if (!error1) {
// NSString *response = [request responseString];
// NSLog(@"Test:%@",response);
// }
// }
相关文章推荐
- form表单数据封装成json格式并提交给服务器的实现方法
- json数据客户端和服务端的处理方法
- 客户端GET方法提交数据,服务器返回文件格式数据的demo
- 客户端传递json格式数据,spring mvc服务端接受并进行参数校验
- 如果希望给Android客户端提供JSON格式的数据,服务端应该用什么技术编写呢?
- 使用JSON格式提交数据到服务端的实例代码
- 用JSON格式提交数据到服务端
- extjs Post方法提交json格式的数据到php
- extjs Post方法提交json格式的数据到php
- Android之提交数据到服务端方法简单封装
- 客户端POST提交、服务器返回json格式数据的demo
- 一种将数据表DataTable转为JSON格式的新方法
- 一种将数据表DataTable转为JSON格式的新方法
- 客户端与服务端的数据交互 PageMethods + JSON
- 客户端 使用XML DOM加载json数据的方法
- ExtJS fileupload组件上传文件后从服务端解析JSON格式数据
- ajax 框架 dojo 提交json数据乱码的解决方法
- xml json两种数据格式后台的封装(asp.net)
- 用Jquery处理PHP返回的JSON格式数据的三种方法
- android使用JSON进行网络数据交换(服务端、客户端)的实现