无法使用AFNetworking使用数组发布字典

时间:2014-12-16 15:42:52

标签: ios objective-c post afnetworking

显然,服务器无法通过数组理解我的字典。 我写了这个

NSURL *url = [NSURL URLWithString:@"http://"];
self.manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:url];

self.manager.requestSerializer = [AFJSONRequestSerializer serializer];

AFJSONResponseSerializer *responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
[self.manager setResponseSerializer:responseSerializer];
self.manager.responseSerializer.acceptableContentTypes = [self.manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];

NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:
                            [SOUser sharedManager].id, @"id",
                            @"vk", @"socialId",
                            [SOUser sharedManager].firstName, @"firstName",
                            [SOUser sharedManager].lastName, @"lastName",
                            [SOUser sharedManager].sex, @"gender",
                            [SOUser sharedManager].friends, @"friends", nil];

[self.manager POST:@"api/user/"
   parameters:parameters
      success:^(AFHTTPRequestOperation *operation, id responseObject) {
          //NSLog(@"Response: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
          NSLog(@"OPERATION = %@", operation);
          NSLog(@"%@", operation.responseString);
          NSLog(@"JSON: %@", responseObject);
          NSLog(@"Token = %@", [responseObject objectForKey:@"token"]);
          [SOUser sharedManager].legendaryToken = [responseObject objectForKey:@"token"];
      } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
          NSLog(@"Error: %@", error);
          NSLog(@"message: %@", [operation.responseObject objectForKey:@"message"]);
          NSLog(@"operation: %@", [operation.responseObject objectForKey:@"errors"]);
          NSDictionary *ac = [operation.responseObject objectForKey:@"errors"];
          NSLog(@"%@", [ac objectForKey:@"email"]);
          NSString *fg = [ac objectForKey:@"email"];
          NSLog(@"%@", fg);
      }];

[SOUser sharedManager].friends NSMutableArray的位置:

for (int i = 0; i < 25; i++) {
    [self.friends addObject:[NSString stringWithFormat:@"%i", i]];
}

在服务器上我收到我的所有数据,而不是我的数字服务器打印字符串“数组”我的数组?它可以是什么?

我的JSON:

JSON: {
    email = "<null>";
    "fb_friends" = "<null>";
    "fb_id" = "<null>";
    firstName = Sergey;
    gender = 2;
    id = 17;
    lastName = Oleynich;
    phone = "<null>";
    token = 6990b71a61411d9d038dad1e2a54dd9f;
    "vk_friends" =     (
        0,
        1,
        2,
        3,
        4,
        5,
        6,
        7,
        8,
        9,
        10,
        11,
        12,
        13,
        14,
        15,
        16,
        17,
        18,
        19,
        20,
        21,
        22,
        23,
        24
    );
    "vk_id" = 3;

服务器

JSON : Id:17
vk_id:3
fb_id:NULL
firstName:Sergey
lastName:Oleynich
gender:2
email:NULL
vk_friends:Array

1 个答案:

答案 0 :(得分:0)

我建议您的请求可能没问题,但问题可能只是您显示&#34;服务器&#34;问题末尾的值。例如,如果您的服务器是用PHP编写的,而echo是包含vk_friends数组的变量,那么它只会显示&#34; Array&#34;。

如果要在PHP中显示数组,可以执行以下操作:

foreach ($vk_friends as $vk_friend) {
    echo $vk_friend . "\r\n";
}

坦率地说,如果您的服务器正在响应该应用,那么您无论如何都可能使用json_encode,这一点没有实际意义。但这可能就是你现在看到&#34; Array&#34;在你的输出中。