AFNetworking returning NSCFData; issue with registerHTTPOperationClass [closed]

匿名 (未验证) 提交于 2019-12-03 01:05:01

问题:

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.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!