gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 03:46:12 27 4
gpt4 key购买 nike

我正在尝试从我的 hudson url 地址获取 JSON 并使用 HTTP 身份验证对我的 (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;
}

我想让计算机的列表显示在我的下拉列表中,但是这两行不能一起工作: [自行设置AuthorizationHeaderWithUsername:userName password:password]; [自行设置DefaultHeader:@"Accept"value:@"application/json"];

如果我只使用第一行,我的身份验证有效,但我收到该错误,因为我试图获取 key :

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.'

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

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

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

蒂亚戈

最佳答案

我在使用 Flickr API 时遇到了这个问题,最终将 AFJSONRequestOperation 子类化,结果证明这是微不足道的。你只需要重写类方法 defaultAcceptableContentTypes 这样,例如:

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

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

[self registerHTTPOperationClass:[CCFFlickrJSONRequestOperation class]];

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

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

关于objective-c - 使用 AFNetworking 通过 HTTP 身份验证获取 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9123862/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com