xcode 如何使用 AFNetworking 发布 JSON 参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34434728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How To POST JSON Parameters Using AFNetworking?
提问by Mohamad Afiq
how can I POST
JSON
parameters with AFNetworking
? Below is the example JSON
format. Thank you in advance.
我该如何POST
JSON
参数AFNetworking
?下面是示例JSON
格式。先感谢您。
{
"application": {
"identifier": "appDev"
},
"sso": {
"version": 2.0,
"session_expiry": 43200
},
"user": {
"password": "xxxx~!@",
"username": "xxx0min"
},
"coordinate": {
"latitude": 10.9948629,
"longitude": -169.5213749
}
}
I successfully created a JSON :
我成功创建了一个 JSON :
NSDictionary *parameters = @{@"application": @{@"identifier": @"appDev"}, @"sso": @{@"version": @"2.0", @"session_expiry":@43200}, @"user": @{@"password":@"xxxx~!@", @"username":@"xxx0min"}, @"coordinate": @{@"latitude":@10.9948629, @"longitude":@-169.5213749}};
回答by Rost
First convert JSON string to NSDictionary:
首先将 JSON 字符串转换为 NSDictionary:
NSError *error;
NSData *objectData = [@"{\"1\":\"2\"}" dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData
options:NSJSONReadingMutableContainers
error:&error];
Then you can send this dictionary with AFNetworking. In version 3 you should use AFHTTPSessionManager:
然后你可以用 AFNetworking 发送这本字典。在第 3 版中,您应该使用 AFHTTPSessionManager:
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
[manager POST:@"your_URL" parameters:json progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"Complete");
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"Fail");
}];
回答by Mohamad Afiq
Finally I found a solution to my problem. What I was doing is making NSDictionary of NSDictionary for my JSON parameters. Below is the full solution code.
最后我找到了解决我的问题的方法。我正在做的是为我的 JSON 参数制作 NSDictionary 的 NSDictionary。下面是完整的解决方案代码。
NSString *loginURL = @"http://myappdev.dev/login";
NSDictionary *parameters = @{@"application": @{@"identifier": @"appDev"}, @"sso": @{@"version": @"2.0", @"session_expiry":@43200}, @"user": @{@"password":@"xxxx~!@", @"username":@"xxx0min"}, @"coordinate": @{@"latitude":@10.9948629, @"longitude":@-169.5213749}};
NSLog(@"params : %@", parameters);
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager POST:loginURL parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON Successsss: %@", responseObject);
NSLog(@"operation Successsss: %@", operation);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error laaa: %@", error);
}];