0

I am trying to post a JSON using AFNetworking.

Here's the code that im using:

+ (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;
}

The following code does not works. Everytime i try i get the following error:

The operation couldn’t be completed. (NSURLErrorDomain error -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:

// 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);
    }];
}

Why the first login method does not works? What am i doing wrong?

4

2 回答 2

0

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

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

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

这当然可能是由于没有正确序列化参数造成的。我建议使用较低级别的 API,手动创建请求,使用 手动对 JSON 进行编码NSJSONSerialization,并设置请求的正文数据和 URL。恕我直言,这当然是更具可读性的代码,并且可能需要更少的代码。

于 2013-09-10T22:43:34.537 回答
0

换个试试

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

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/*HERE THE URL STRING TO CALL*/"]]
于 2013-09-10T15:15:49.533 回答