iOS开发往服务器Post一个json数据
2016-05-18 11:45
337 查看
有时候需要往服务器Post一个json数据。
比如我传的是一个联系人数组。
然后往模型里面添加数组之前。先调用modelToString进行序列号就好。
参数里面可以直接使用数组。
比如
使用AFNetWorking。
推荐使用AFNetworking。自己的话还需要加一些其它的设置,具体我还要研究。
博客地址 iseedog
参数
参数可以被自动序列号。前提是要是iOS里面的数据类型,比如NSDictionary,NSArray,NSNumber,NSString等。如果是自己的模型数组,可以自己写一个模型的ToString方法,网上似乎也有些库,不过自己写起来也不麻烦。比如我传的是一个联系人数组。
@interface ContactModel : NSObject @property (nonatomic, strong) NSString *name; @property (nonatomic, strong) NSString *contact_phone; @property (nonatomic, strong) NSString *created_at; - (NSString *)modelToString; @end - (NSString *)modelToString{ return [NSString stringWithFormat:@"{\"contact_phone\":\"%@\", \"name\":\"%@\", \"source\":1, \"created_at\":\"%@\"}",_contact_phone,_name,_created_at]; } @end
然后往模型里面添加数组之前。先调用modelToString进行序列号就好。
参数里面可以直接使用数组。
比如
NSMutableArray *array = [ContactsHelper contactArray]; NSDictionary *parmeters = @{@"user_id":@"2222", @"contact":array };
请求
使用原生的http请求。-(void)request: (NSString*)httpUrl withHttpArg: (NSDictionary*)parameters{ NSURL *url = [NSURL URLWithString: httpUrl]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 10]; [request setHTTPMethod: @"POST"]; if (parameters) { if (![mutableRequest valueForHTTPHeaderField:@"Content-Type"]) { [mutableRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; } [mutableRequest setHTTPBody:[NSJSONSerialization dataWithJSONObject:parameters options:self.writingOptions error:error]]; } [NSURLConnection sendAsynchronousRequest: request queue: [NSOperationQueue mainQueue] completionHandler: ^(NSURLResponse *response, NSData *data, NSError *error){ if (error) { NSLog(@"Httperror: %@%ld", error.localizedDescription, error.code); } else { NSInteger responseCode = [(NSHTTPURLResponse *)response statusCode]; NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"HttpResponseCode:%ld", responseCode); NSLog(@"HttpResponseBody %@",responseString); NSJSONSerialization *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; NSDictionary *dicjson = (NSDictionary *)json; dispatch_async(dispatch_get_main_queue(), ^{ //刷新UI }); } }]; }
使用AFNetWorking。
NSMutableURLRequest *request = [[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString: parameters:parmeters error:nil]; [NSURLConnection sendAsynchronousRequest: request queue: [NSOperationQueue mainQueue] completionHandler: ^(NSURLResponse *response, NSData *data, NSError *error){ if (error) { NSLog(@"Httperror: %@%ld", error.localizedDescription, error.code); } else { NSInteger responseCode = [(NSHTTPURLResponse *)response statusCode]; NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"HttpResponseCode:%ld", responseCode); NSLog(@"HttpResponseBody %@",responseString); NSJSONSerialization *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; NSDictionary *dicjson = (NSDictionary *)json; NSLog(@"%@",dicjson); dispatch_async(dispatch_get_main_queue(), ^{ //刷新UI }); } }];
推荐使用AFNetworking。自己的话还需要加一些其它的设置,具体我还要研究。
博客地址 iseedog
相关文章推荐
- 小心服务器内存居高不下的元凶--WebAPI服务
- 运维入门
- 使用 Python 处理 JSON 格式的数据
- XML 与 JSON 优劣对比
- 利用开源软件打造自己的全功能远程工具
- Linux5.9无人值守安装
- 数据中心和云未来的十二大趋势
- 用vsftp快速搭建ftp服务器
- Linux快速构建apache web服务器
- 服务器监控策略浅谈
- VBA将excel数据表生成JSON文件
- 如何降低服务器采购成本 原理分析
- 建议的服务器分区办法
- 服务器托管六大优势分析
- Erlang实现的一个Web服务器代码实例
- 服务器技术全面解析
- 保护DNS服务器的几点方法小结
- newtonsoft.json解析天气数据出错解决方法
- 我国成为全球第二大服务器消费国