Estoy tratando de obtener un JSON de mi dirección url de hudson y autenticar mi aplicación (Mac OS X) utilizando la Autenticación HTTP.Obtener JSON con Autenticación HTTP con AFNetworking
Siguiendo el ejemplo que estoy usando:
// 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;
}
quiero obtener la lista del equipo para mostrar en mi desplegable, pero estas dos líneas no trabaja en conjunto: [auto setAuthorizationHeaderWithUsername: nombre de usuario: contraseña ]; [self setDefaultHeader: @ "Aceptar" valor: @ "application/json"];
Si sólo tiene que utilizar la primera línea, mis obras authetication, pero recibo que el error porque trato de obtener una clave:
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.'
Si usar la segunda línea, mi autenticación devolverá un error 403.
¿Alguien podría ayudar con el problema?
Gracias y disculpas por cualquier error en inglés.
Thiago
¿Qué sucede cuando usa ambas líneas, exactamente? – mattt
@mattt, el error me dijo que faltaba un tipo de contenido, así que, he agregado una tercera línea: [self setDefaultHeader: @ "Content-Type" value: @ "application/json"]; y el error es: Código de error = Dominio com.alamofire.networking.error = -1,016 "Tipo de contenido esperado {( "text/javascript", "application/json", "text/json" )}, obtuvo application/javascript "UserInfo = 0x100513190 {NSLocalizedDescription = Tipo de contenido esperado {( " text/javascript ", " application/json ", " text/json " )}, obtuvo application/javascript, NSErrorFailingURLKey = http://hudson.concretecorp.com.br/computer/api/json} – unnamedd