网络:提交 JSON 到服务器中
来源:互联网 发布:vr软件app排行榜 编辑:程序博客网 时间:2024/05/16 23:52
#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.}- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { // NSURL NSURL *url = [NSURL URLWithString:@"http://localhost/post/postjson.php"]; // NSURLRequest NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; // 设置HTTP方法 [request setHTTPMethod:@"POST"]; NSDictionary *dict = @{ @"name":@"zhangsan" }; /* - Top level object is an NSArray or NSDictionary 顶级节点必须是数据或者字典 - All objects are NSString, NSNumber, NSArray, NSDictionary, or NSNull 所有对象都必须是这几个类型 - All dictionary keys are NSStrings 字典的key值必须是strng类型 - NSNumbers are not NaN or infinity */ //nil 0x0 // null 没有 long long value方法 NSArray *jsons = @[@1,@2,[NSNull null],@"hello"]; // 如果使用数组包装了,虽然能上传,但是返回的结果并不是我们想要的字典// NSString *json = @"{\"name\":\"zhangsan\"}"; // 验证一下,是否符合JSON格式的要求 if (![NSJSONSerialization isValidJSONObject:jsons]) { NSLog(@"不是JSON格式"); return; } // 从对象转化成JSON数据 // obj 就是要转成json的对象,必须是数组或者字典 NSData *data = [NSJSONSerialization dataWithJSONObject:jsons options:0 error:NULL]; // 向服务器提交一个json request.HTTPBody = data; // NSURLConnection [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) { NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]); NSLog(@"%@",[NSJSONSerialization JSONObjectWithData:data options:0 error:NULL]); }];}@end
0 0
- 网络:提交 JSON 到服务器中
- Jquery Post提交JSON主从关系对象数组到服务器
- android-async-http json字符串提交到服务器
- Jquery Post提交JSON主从关系对象数组到服务器
- ios-day23-02(ios开发之json的反序列化、提交json数据到服务器)
- 提交表单到服务器
- 提交数据到服务器
- git提交到服务器
- android-----提交数据到服务器的4中方式
- URL中带的中文提交到服务器成乱码
- Android开发中 提交patch到服务器的操作流程
- iOS开发中上传JSON字符串到后台服务器
- 将字符串json写入到服务器文件中
- Android 提交数据到服务器
- Android提交数据到服务器
- Android提交请求到服务器
- git 提交到远程服务器
- 5.1提交数据到服务器
- iOS 动画Animation-4-5: CALayer子类:CATransformLayer
- Cocos格子地图(3)
- 获取html页面中某个父元素下相同子元素的个数
- Altium Designer-Net has no driving source警告消除的方法
- 搭建自己的Git服务器
- 网络:提交 JSON 到服务器中
- 为什么十跪父母恩?
- 刚毕业没几年,别去计较太多得失
- hadoop是什么?
- LeetCode:Valid Parentheses
- Java 详解instanceof
- CocosUI之九妹图和编辑框
- Linux Java JDK安装方式记录
- ZOJ-3469-Food Delivery(区间DP)