AFNetworking Array在块内但不在块外返回JSON [英] AFNetworking Array returns JSON inside of block but not outside of block
问题描述
我创建了一个自定义方法,该方法使用AFNetworking API返回JSON数据。
I've created a custom method that returns JSON data using the AFNetworking API.
我试图将从Web服务提取的数据存储在Array中, JSON。 NSLog(@%@,json);块内将JSON数据打印到控制台。在块NSLog(@%@,json)之外;返回null。为什么会这样,如何解决?
I'm trying to store data pulled from my web service in an Array as JSON. NSLog(@"%@", json); inside the block prints the JSON data to console. Outside the block NSLog(@"%@", json); return null. Why is this and how can it be fixed?
我要做的就是获取返回JSON数据(NSArray)的方法
What I'm trying to do is to get the method to return the JSON data (NSArray)
#import "WebService.h"
#import "AFNetworking.h"
#import "MBProgressHUD.h"
@implementation WebService
- (NSArray *)postRequest:(NSDictionary *)postParameters {
//MBProgressHUD *progressHUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//progressHUD.labelText = @"Loading";
__block NSArray *json;
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:@"http://192.168.0.100/app.php" parameters:postParameters success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSString *responseString = [operation responseString];
NSError *error;
json = [NSJSONSerialization
JSONObjectWithData:[responseString dataUsingEncoding:NSUTF8StringEncoding]
options:kNilOptions
error:&error];
NSLog(@"%@", json); // RETURNS JSON
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Network Unavailable"
message:@"Unable to contact server. Please try again later."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
NSLog(@"Error: %@", error);
}];
NSLog(@"%@", json); // RETURNS NULL
return json;
}
@end
推荐答案
由于 POST
是异步运行的,因此无法返回结果。但是您可以采用与AFNetworking相同的完成块模式:
Because POST
runs asynchronously, it's not possible to return the results. But you can employ the same completion block pattern that AFNetworking does:
- (void) postRequest:(NSDictionary *)postParameters success:(void (^)(id jsonObject))success failure:(void (^)(NSError *error))failure {
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:@"http://192.168.0.100/app.php" parameters:postParameters success:^(AFHTTPRequestOperation *operation, id responseObject)
{
if (success)
success(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Network Unavailable"
message:@"Unable to contact server. Please try again later."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
if (failure)
failure(error);
}];
}
您将其称为:
[self postRequest:postParameters success:^(id jsonObject) {
NSLog(@"json = %@", jsonObject);
} failure:^(NSError *error) {
NSLog(@"error = %@", error);
}];
注意,我已经大大简化了代码,因为您不必进行JSON解析,因为AFNetworking已经为您做到了(因为默认的 responseSerializer
是 AFJSONResponseSerializer
)。因此,如您所见,您的上述方法仅比标准AFNetworking POST
方法做得更多,该方法显示了 UIAlertView
如果有错误。
Note, I've simplified your code significantly, because you don't have to do the JSON parsing, because AFNetworking already does that for you (because the default responseSerializer
is AFJSONResponseSerializer
). So as you can see, your above method doesn't do much more than the standard AFNetworking POST
method which shows a UIAlertView
if there was an error.
这篇关于AFNetworking Array在块内但不在块外返回JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!