我正在使用AFNetworking 2.0并遇到了问题。
我正在尝试将json中的数组输入服务器。
我发布的数组就像(见下面的代码)。我将以下数组添加到Json params以发送到服务器:
themes = (
"Fashion - Men",
Kids,
"Styling / Hair"
);
然后发送到服务器:
{
id = 654;
tags = test;
themes = (
"Fashion - Men",
Kids,
"Styling / Hair"
);
}
然而,服务器接收的json就像错误的那样:
{'id': '654', 'themes[]': ['Kids', 'Styling / Hair', 'Accessories - Women'], 'tags': 'test'}
AFNetworking在发送的json中放置[]方括号,如主题[],所以cal是正确的,除了这个:themes []
不知道如何解决这个问题。
- (void) piccMedia: (NSString*) aPiccId Tags: (NSString *) aTags Themes: (NSMutableArray *) anThemes
success:(void (^)(NSArray *response))success
failure:(void (^)(NSError *error))failure
{
NSMutableDictionary * params = [[NSMutableDictionary alloc] init];
[params setObject: @"654" forKey:@"id"];
[params setObject: aTags forKey:@"tags"];
[params setObject: anThemes forKey:@"themes"];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *piccingAuthToken = [prefs stringForKey:@"piccingAuthToken"];
NSString *piccingUsername = [prefs stringForKey:@"piccingUsername"];
NSLog(@"piccingAuthToken %@ %@", piccingAuthToken,piccingUsername);
NSString *postQueryString = [NSString stringWithFormat:@"http://dev.com/services/rest/profiles/%@/media",piccingUsername];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [JSONResponseSerializerWithData serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/plain", @"text/html", nil];
manager.responseSerializer = [JSONResponseSerializerWithData serializerWithReadingOptions:NSJSONReadingAllowFragments | NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves];
[manager.requestSerializer setValue:piccingAuthToken forHTTPHeaderField:@"x-authtoken"];
[manager PUT:postQueryString parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
success(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
id json = error.userInfo [JSONResponseSerializerWithDataKey];
failure(json);
}];
}
2014-02-24 16:49:25.265 App[7314:70b] JSON Parameters{
id = 654;
tags = test;
themes = (
"Accessories - Women",
"Styling / Hair",
Kids
);
}
答案 0 :(得分:0)
您应该将requestSerializer设置为AFJSONRequestSerializer
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager setRequestSerializer:[AFJSONRequestSerializer serializer]];
[manager POST:@"http://example.com/resources.json" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];