2

我是 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

但我希望它成为一本我可以操纵的字典,这就是我认为它应该如何工作的方式?我究竟做错了什么?

4

3 回答 3

5
[httpClient defaultValueForHeader:@"Accept"];

应该:

[httpClient setDefaultHeader:@"Accept" value:@"application/json"];
于 2013-03-21T22:15:07.393 回答
2

是的,responseObject是一个NSData. 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对象将是 aNSDictionaryNSArray,具体取决于您从 API 获得的响应类型。

于 2013-03-21T22:19:25.160 回答
0

我究竟做错了什么?

NSStringFromClass()返回作为NSString对象传入的类的名称。

如果你想从返回的 JSON 字符串中创建一个字典,那么你必须解析它,例如使用NSJSONSerialization类。

于 2013-03-21T22:04:20.823 回答