可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am new to AFNetworking and am making a call to a simple login api that returns json like:
{"status":"success","data":{"auth_token":"12jt34"}}
I'm doing it via the following but it is returning __NSCFData rather than something that I can manipuate.
NSURL *baseURL = [NSURL URLWithString:@"http://localhost:3000/arc/v1/api/"]; AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:baseURL]; [httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]]; [httpClient defaultValueForHeader:@"Accept"]; NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: uname,@"email", pwd, @"password", nil]; [httpClient postPath:@"login-mobile" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { NSString *className = NSStringFromClass([responseObject class]); NSLog(@"val: %@",className); }failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error retrieving data: %@", error); }];
and it outputs:
2013-03-21 14:52:51.290 FbTabbed[21505:11303] val: __NSCFData
but I'd like it for it to be a dictionary that I can manipulate which is how I think it is supposed to work? What am I doing wrong?
回答1:
[httpClient defaultValueForHeader:@"Accept"];
should be:
[httpClient setDefaultHeader:@"Accept" value:@"application/json"];
回答2:
Yes, responseObject is a NSData. You can then parse it into a dictionary or array using NSJSONSerialization method JSONObjectWithData:
NSURL *baseURL = [NSURL URLWithString:@"http://localhost:3000/arc/v1/api/"]; AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:baseURL]; [httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]]; [httpClient defaultValueForHeader:@"Accept"]; NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: uname,@"email", pwd, @"password", nil]; [httpClient postPath:@"login-mobile" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { NSAssert([responseObject isKindOfClass:[NSData class]], @"responseObject is supposed to be a NSData"); // it should be a NSData class NSError *error; self.results = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:&error]; if (error != nil) { // handle the error // an example of the sort of error that could result in a parse error // is if common issue is that certain server errors can result in an // HTML error page (e.g. you have the URL wrong, your server will // deliver a HTML 404 page not found page). If you want to look at the // contents of the `responseObject`, you would: // // NSLog(@"responseObject=%@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]); } }failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error retrieving data: %@", error); }];
Obviously, your results object would be a NSDictionary or NSArray, depending upon the type of response you get from your API.
回答3:
What am I doing wrong?
You're making assumptions. And what's even worse, you don't bother reading the documentation. NSStringFromClass() ain't no magic. It returns the name of the class you pass in as an NSString object. If you want to make a dictionary out of the returned JSON string, then you have to parse it, for example using the NSJSONSerialization class.