将NSArray作为AFNetworking中的参数传递

时间:2016-06-13 08:23:05

标签: ios objective-c afnetworking

如何将NSArray作为AFNetworking中的参数传递?

我要调用的API期待列表(数组)。 我不能使用字典,因为没有密钥。

2 个答案:

答案 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"];
  }

}