के साथ HTTP प्रमाणीकरण के साथ JSON प्राप्त करें मैं अपने हडसन यूआरएल पते से JSON प्राप्त करने का प्रयास कर रहा हूं और HTTP प्रमाणीकरण का उपयोग कर अपने (मैक ओएस एक्स) एप्लिकेशन को प्रमाणित करता हूं।AFNetworking
उदाहरण मैं उपयोग कर रहा हूँ के बाद:
// 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: USERNAME पासवर्ड: पासवर्ड ]; [स्वयं सेटडिफॉल्ट हैडर: @ "स्वीकार करें" मान: @ "एप्लिकेशन/जेसन"];
अगर मैं सिर्फ पहली पंक्ति, मेरे 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
@mattt, त्रुटि ने मुझे बताया कि सामग्री-प्रकार गायब था, इसलिए, मैंने एक तीसरी पंक्ति जोड़ा है: [स्वयं सेटडिफॉल्ट हैडर: @ "सामग्री-प्रकार" मान: @ "एप्लिकेशन/जेसन"]; और त्रुटि है: त्रुटि डोमेन = com.alamofire.networking.error कोड = -1016 "अपेक्षित सामग्री प्रकार {( " text/javascript ", " application/json ", " text/json " )}, मिल गया आवेदन/javascript "UserInfo = 0x100513190 {NSLocalizedDescription = अपेक्षित सामग्री प्रकार {( " text/javascript ", " application/json ", " text/json " )}, आवेदन/जावास्क्रिप्ट मिला, NSErrorFailingURLKey = http://hudson.concretecorp.com.br/computer/api/json} – unnamedd