我想從我的哈德森url地址獲取JSON並使用HTTP驗證身份驗證我的(Mac OS X)應用程序。通過使用AFNetworking的HTTP身份驗證獲取JSON
繼我使用的例子:
// 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;
}
我想要得到的計算機列表在我的下拉列表顯示,但此兩行不一起工作: [自setAuthorizationHeaderWithUsername:用戶名密碼:密碼]。 [self setDefaultHeader:@「Accept」value:@「application/json」];
如果我只是用第一行,我authetication的作品,但我收到的錯誤,因爲我試圖得到一個關鍵:
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。
任何人都可以幫助解決問題嗎?
謝謝併爲任何英文錯誤表示歉意。
蒂亞戈
當你同時使用兩條線時會發生什麼? – mattt 2012-02-03 05:14:38
@mattt,錯誤告訴我缺少內容類型,所以我添加了第三行: [self setDefaultHeader:@「Content-Type」value:@「application/json」]; 和錯誤是: 錯誤域= com.alamofire.networking.error代碼= -1016「預期內容類型{( 「文本/ JavaScript的」, 「應用/ JSON」, 「文本/ JSON」 )},得到了application/javascript「UserInfo = 0x100513190 {NSLocalizedDescription =期望的內容類型{( 」text/javascript「, 」application/json「, 」text/json「 )},獲取應用/ javascript,NSErrorFailingURLKey = http://hudson.concretecorp.com.br/computer/api/json} – unnamedd 2012-02-03 05:39:17