AFNetworking返回NSCFData; registerHTTPOperationClass的问题

时间:2013-03-21 21:57:19

标签: ios afnetworking

我是AFNetworking的新手,正在调用一个返回json的简单登录api:

{"status":"success","data":{"auth_token":"12jt34"}}

我是通过以下方式完成的,但它返回__NSCFData而不是我可以操作的东西。

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);
}];

并输出:

2013-03-21 14:52:51.290 FbTabbed[21505:11303] val: __NSCFData

但是我喜欢它,因为它是一个我可以操作的字典,我认为它应该如何工作?我做错了什么?

3 个答案:

答案 0 :(得分:4)

[httpClient defaultValueForHeader:@"Accept"];

应该是:

[httpClient setDefaultHeader:@"Accept" value:@"application/json"];

答案 1 :(得分:2)

是的,responseObjectNSData。然后,您可以使用NSJSONSerialization方法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);
}];

显然,您的results对象可能是NSDictionaryNSArray,具体取决于您从API获得的回复类型。

答案 2 :(得分:0)

  

我做错了什么?

你在做假设。更糟糕的是,你不打扰阅读文档。 NSStringFromClass()并非魔术。它将您传入的类的名称作为NSString对象返回。如果要从返回的JSON字符串中创建字典,则必须解析它,例如使用NSJSONSerialization类。