_dataDictionary = [[NSMutableDictionary alloc]initWithCapacity:0];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json; charset=utf-8",@"text/json", @"text/plain", nil];
// NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/ordershow/frontPage.json?pageNo=1&pageSize",IP_URL]];
[manager GET:url.absoluteString parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSString *str = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
_dataDictionary = [[[SBJsonParser alloc] init] objectWithString:str];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"------------ç---------- shi bai -----------------%@",error);
}];
//输出请求的数据
NSLog(@"------------%@",_dataDictionary);
为什么不直接把 NSLog(@"------------%@",_dataDictionary)
放到 block 里面?
看懂了,AFN的异步请求成功调用block,所以你现在的写法取到的是nil。
如果想外面取得block内部的数据,可以写个block把这个成功回调传出来
/*
*void (^)(ResultModel *))block 供数据返回
*data 需要提交给接口的数据
/
+ (void)getBlock:(void (^)(ResultModel *))block andPostData:(NSString*)data{
// AFN代码
// 执行AFN接口解析,并且在AFN回调里执行下面的代码
if (block) {
ResultModel *result = [[ResultModel alloc] initWithDict:(NSDictionary*)JSON];
block(result);//我们使用一个block回调把获取到的数据传递出来。
}
}