AFJSONRequestOperation不起作用(postpath工作)

时间:2013-09-10 15:00:21

标签: ios objective-c afnetworking afjsonrequestoperation

我正在尝试使用AFNetworking发布JSON。

以下是我使用的代码:

+ (RESTAPI *)sharedClient
{
    static RESTAPI *_sharedClient = nil;
    static dispatch_once_t oncePredicate;
    dispatch_once(&oncePredicate, ^{
        _sharedClient = [[self alloc] initWithBaseURL:[NSURL URLWithString:@"https://mybaseurl.com"]];
    });

    return _sharedClient;
}

- (id)initWithBaseURL:(NSURL *)url
{
    self = [super initWithBaseURL:url];
    if (!self) {
        return nil;
    }

    [self setParameterEncoding:AFJSONParameterEncoding];
    [self registerHTTPOperationClass:[AFJSONRequestOperation class]];
    [self setDefaultHeader:@"Accept" value:@"application/json"];
    [self setAllowsInvalidSSLCertificate:YES];

    return self;
}

以下代码不起作用。每次我尝试我都会收到以下错误:

操作无法完成。 (NSURLErrorDomain错误-1012。)

// this code does not works
// 
- (void)loginNOTWORKING
{
    RESTAPI *client = [RESTAPI sharedClient];

    [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
    [[AFNetworkActivityIndicatorManager sharedManager] incrementActivityCount];

    NSDictionary *parameter = @{@"tgout": @"1",
                                @"tgin": @2,
                                @"username": @"foo",
                                @"password":@"bar"};
    NSURLRequest *request = [client requestWithMethod:@"POST" path:@"/login" parameters:parameter];

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        // code for successful return goes here
        [[AFNetworkActivityIndicatorManager sharedManager] decrementActivityCount];
        NSLog(@"THIS IS NEVER CALLED: %@", JSON);
        // do something with return data
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
        // code for failed request goes here
        [[AFNetworkActivityIndicatorManager sharedManager] decrementActivityCount];
        NSLog(@"SAD, VERY SAD: %@", error.localizedDescription);
        // do something on failure
    }];

    [operation start];
}

此代码有效:

// this code WORKS
- (void)loginWORKING
{
    RESTAPI *client = [RESTAPI sharedClient];

    [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
    [[AFNetworkActivityIndicatorManager sharedManager] incrementActivityCount];

    NSDictionary *parameter = @{@"tgout": @"1",
                                @"tgin": @2,
                                @"username": @"foo",
                                @"password":@"bar"};

    [client postPath:@"/login" parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
        // Print the response body in text
        NSLog(@"IT WORKS: %@",responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Response: %@", error.localizedDescription);
    }];
}

为什么第一种登录方法不起作用?我做错了什么?

2 个答案:

答案 0 :(得分:0)

尝试替换

NSURLRequest *request = [client requestWithMethod:@"POST" path:@"/login" parameters:parameter];

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/*HERE THE URL STRING TO CALL*/"]]

答案 1 :(得分:0)

您可以在文件CFNetworkErrors.h中找到错误-1012:

kCFURLErrorUserCancelledAuthentication = -1012
"The connection failed because the user cancelled required authentication."

我想,您的身份验证存在问题。错误描述可能会误导“用户” - 它实际上是一个被调用的委托方法,它取消了身份验证,或者身份验证失败了。

这当然可能是因为没有正确地序列化参数。我建议使用较低级别的API,手动创建请求,使用NSJSONSerialization手动编码JSON,并设置正文数据和请求的URL。恕我直言,这肯定是更易读的代码,可能需要更少的代码。