如何将NSArray
作为AFNetworking
中的参数传递?
我要调用的API
期待列表(数组)。
我不能使用字典,因为没有密钥。
答案 0 :(得分:2)
AFHTTPRequestOperationManager *manager;
manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setTimeoutInterval:500.0];
[manager POST:strRequestUrl parameters:yourArray success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSString *msg;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:responseObject
options:NSJSONWritingPrettyPrinted
error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
答案 1 :(得分:1)
//而不是数组u可以发送JSON字符串作为输入参数
NSMutableDictionary *dicinput = [[NSMutableDictionary alloc] init];
if ([NSJSONSerialization isValidJSONObject:arrayToConvert]) {
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:arrayToConvert
options:0
error:&error];
NSString *JSONString;
if (!jsonData) {
NSLog(@"JSON error:%@", error);
} else {
JSONString = [[NSString alloc] initWithBytes:[jsonData bytes] length:[jsonData length] encoding:NSUTF8StringEncoding];
[dicinput setValue:JSONString forkey:@"input"];
}
}