AFNetworking没有得到适当的回应

时间:2013-10-29 04:35:48

标签: ios rest afnetworking

我正在尝试使用AFNetworking来调用Rest API,但我没有得到正确的响应字符串。这是我的代码:

NSURL *url = [[NSURL alloc] initWithString:@"https://www.ez-point.com/api/v1/ezpoints"];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];

    AFJSONRequestOperation *operation =
    [AFJSONRequestOperation JSONRequestOperationWithRequest:request
        success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
            NSLog(@"%@",@"testing");
            NSLog(@"%@",operation.responseData);

        }
            failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
                NSLog(@"%@", @"Error");
        }];

    [operation start];

但是我打印出这个:

2013-10-29 08:31:08.175 EZ-POINT[4004:c07] testing
2013-10-29 08:31:08.175 EZ-POINT[4004:c07] (null)

如您所见,它返回null,我期待这样:

{"status": "user_invalid", "data": "", "token": "", "errors": ["user not found"], "user_id": ""}

1 个答案:

答案 0 :(得分:1)

我更习惯于这种设置请求的方式:

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.ez-point.com/api/v1/ezpoints"]];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request 
    success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        NSLog(@"%@",@"testing");
        NSLog(@"%@",operation.responseData);

    }
    failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
        NSLog(@"%@", @"Error");
    }];

[operation start];