使用AFNetworking通过HTTP身份验证获取JSON

unn*_*edd 5 objective-c afnetworking

我正在尝试从我的hudson url地址获取JSON并使用HTTP Authenticate验证我的(Mac OS X)应用程序.

按照我正在使用的示例:

// AppDelegate.m
- (void) doSomething {
    [[CommAPIClient sharedClient] getPath:@"/computer/api/json" parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Name: %@", [responseObject valueForKeyPath:@"totalExecutors"]);

    } failure:nil];
}

// CommAPIClient.m
+ (CommAPIClient *) sharedClient {
    static CommAPIClient *_sharedClient = nil;
    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{
        AppDelegate *appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate];
        _sharedClient = [[self alloc] initWithBaseURL:[NSURL URLWithString:    [appDelegate.hudsonTextField stringValue]]];
    });
    return _sharedClient;
}

- (id) initWithBaseURL:(NSURL *)url {
    self = [super initWithBaseURL:url];
    if (self){
        [self registerHTTPOperationClass:[AFJSONRequestOperation class]];

        AppDelegate *appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate];
        userName = [appDelegate.userTextField stringValue];
        password = [appDelegate.passwdTextField stringValue];

        [self setAuthorizationHeaderWithUsername:userName password:password];
        [self setDefaultHeader:@"Accept" value:@"application/json"];
    }
    return self;
}
Run Code Online (Sandbox Code Playgroud)

我想让计算机的列表显示在我的下拉列表中,但这两行不能同时工作:[self setAuthorizationHeaderWithUsername:userName password:password]; [self setDefaultHeader:@"Accept"value:@"application/json"];

如果我只使用第一行,我的验证工作,但我收到错误,因为我试图得到一个键:

2012-02-03 02:43:57.542 HudsonSlave[7523:707] An uncaught exception was raised
2012-02-03 02:43:57.542 HudsonSlave[7523:707] [<NSConcreteData 0x100850000> valueForUndefinedKey:]: this class is not key value coding-compliant for the key totalExecutors.

2012-02-03 02:43:57.623 HudsonSlave[7523:707] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSConcreteData 0x100850000> valueForUndefinedKey:]: this class is not key value coding-compliant for the key totalExecutors.'
Run Code Online (Sandbox Code Playgroud)

如果使用第二行,我的身份验证将返回错误403.

有人可以帮忙解决问题吗?

感谢并为英语中的任何错误道歉.

蒂亚戈

Flu*_*imp 14

我使用Flickr API遇到了这个问题并最终进行了子类化AFJSONRequestOperation,结果证明是微不足道的.您只需要覆盖类方法defaultAcceptableContentTypes,例如:

+ (NSSet *)defaultAcceptableContentTypes;
{
    return [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/plain", nil];
}
Run Code Online (Sandbox Code Playgroud)

然后在我的AFHTTPClient子类中,我只是将我的子类注册为默认的HTTP操作类:

[self registerHTTPOperationClass:[CCFFlickrJSONRequestOperation class]];
Run Code Online (Sandbox Code Playgroud)

更新2013-09-09 14-52-47:

方法名称现在是 + (NSSet *)acceptableContentTypes

  • 它看起来像`AFNetworking 1.0RC1`这个方法名称已改为`+(NSSet*)acceptableContentTypes` (5认同)

mat*_*ttt 7

"预期的内容类型{("text/javascript","application/json","text/json")},得到了application/javascript"

就像错误说的那样,请求是期望的text/javascript,application/json或者text/json,但是服务器发送了application/javascript(这是一个不正确的mime mime类型).

让服务器以正确的mime类型响应,或者(可能更容易)手动更改AFJSONRequestOperation指定哪些mime类型可访问的行.改变涉及子类化的库代码有一个不太常见的替代方法,但这可能比你的情况下的麻烦要多得多.


小智 5

实际上,您现在可以轻松添加更多内容类型:

[AFHTTPRequestOperation addAcceptableContentTypes:[NSSet setWithObjects:@"text/html", nil]];
Run Code Online (Sandbox Code Playgroud)