iOS AFJSONRequestOperation返回空结果

时间:2013-12-27 10:53:00

标签: ios objective-c json xcode afnetworking

我正在尝试使用AFJSONRequestOperation从JSON请求中检索数据。 成功后,我能够成功检索数据但无法完成请求并进一步转发数据以进行处理。

以下是我的代码

-(void) retrieveBrandList:(void (^)(NSArray *brandList))success failure:(void (^)(NSError *error))failure
{
    //__block NSArray *brandList =[[NSArray alloc] init];

    NSString *BrandListURL= http://127.0.0.1:8888/know/rest/brand
    NSURLRequest *request = [NSURLRequest requestWithURL:url];


    NSLog(@"Brand List URL = %@", BrandListURL);

    AFJSONRequestOperation *operation =[AFJSONRequestOperation
                                        JSONRequestOperationWithRequest: request
                                        success:^(NSURLRequest *request, NSHTTPURLResponse *response, id responseObject)
                                        {
                                            NSLog(@"%@", responseObject);
                                            brandList = [self successBandList:responseObject]; // parsing the JSON response in separate method (success block code)
                                            if (success)
                                                success(brandList);
                                        }

                                        failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id responseObject)
                                        {
                                        message:[NSString stringWithFormat:@"%@",error];
                                            if (failure)
                                                failure(error);
                                        }];

    [operation start];
    [operation waitUntilFinished];

}

以下是检索数据的数据管理器。

- (NSArray *)getBrandList
{
    @try
    {

       [brand retrieveBrandList:^(NSArray *brandList)
        {
        brands = brandList;
        }
        failure:^(NSError *error) {
        }];

        NSLog(@"Retriving Brand list completed");
        return brands;
    }

    @catch (NSException * e) {
        NSLog(@"Exception: %@ , Error while getting the brand list", e);
    }

    return NULL;

}

如何完成操作并使用或存储结果以便在其他方法中进一步处理?

1 个答案:

答案 0 :(得分:0)

id jsonObject = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:&error];
        if ([jsonObject isKindOfClass:[NSDictionary class]]) {
            self.jsonDictionary = jsonObject;
        }

你可以检查其他选项,但这对我有用