使用AFNetworking 2.0发布JSON数据
我有一个网页脚本接受JSONstring作为input通过HTTP POST请求。 我曾经遇到过几个AFNetworking 1.x的例子,请大家指点一下,或者给AFNetworking 2.0的例子做一个HTTP POST请求到带有格式化JSON的web脚本作为input?
谢谢
在search文档并尝试了一些代码后,我作为一个例子
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.requestSerializer = [AFJSONRequestSerializer serializer]; NSDictionary *params = @ {@"user" :txtUserName, @"pwd" :txtPwd }; [manager POST:URL_SIGNIN parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"JSON: %@", responseObject); } failure: ^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error); }];
另外不要忘了在服务器脚本中设置应答头types为Application / json。
这里是以JSON格式存储在NSMutableDictionary
参数中的POST参数的一个简单模板。 适用于AFNetworking 2.4.1。
NSString *baseURL = @"http://your-server.com/"; NSString *path = @"method/url/"; NSMutableDictionary *parameters = [NSMutableDictionary dictionary]; [parameters setObject:@"value" forKey:@"key"]; AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL]; manager.requestSerializer = [AFJSONRequestSerializer serializer]; manager.responseSerializer = [AFJSONResponseSerializer serializer]; [manager POST:path parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) { NSLog(@"JSON: %@", responseObject); //here is place for code executed in success case } failure:^(NSURLSessionDataTask *task, NSError *error) { //here is place for code executed in error case UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error while sending POST" message:@"Sorry, try again." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alertView show]; NSLog(@"Error: %@", [error localizedDescription]); }];
如果你想发布json到服务器,你应该使用下面的这种方式发布你的参数。
使用这种方法,你可以在你的请求头中find“Content-Type”为“application / json”。
AFJSONRequestSerializer *serializer = [AFJSONRequestSerializer serializer]; [serializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; [serializer setValue:@"application/json" forHTTPHeaderField:@"Accept"]; manager.requestSerializer = serializer; NSDictionary *paras = @{@"uid" : @(10020)}; [manager POST:@"http://your.request.url" parameters:paras success:^(AFHTTPRequestOperation *operation, id responseObject) { }failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"the falire is %@", error); }];
愿这帮助。 🙂
在迅速:
将其添加到manager.AFHTTPRequestOperationManager
manager.requestSerializer = AFJSONRequestSerializer(writingOptions: NSJSONWritingOptions.PrettyPrinted)
如果你想发布参数字典jsonstring使用下面的代码
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"your_webservice_post_url"]]; [urlRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"]; [urlRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; NSError *error; NSDictionary *parameters = @{@"customValue":@"value"}; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameters options:NSJSONWritingPrettyPrinted error:&error]; [urlRequest setHTTPMethod:@"POST"]; [urlRequest setHTTPBody:jsonData];