Json 加速器和 AFNetworking
Posted
技术标签:
【中文标题】Json 加速器和 AFNetworking【英文标题】:Json Accelerator and AFNetworking 【发布时间】:2013-03-26 20:24:59 【问题描述】:当操作运行时,我无法在由加速器创建的 JSON 模型中输入数据。 你能告诉我我做错了什么吗?
[super viewDidLoad];
NSLog(@"you are in a tableViewController");
self.title = @"NavigationOrdini";
NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.stampa6x3.com/json.php?azione=ordini"]];
AFJSONRequestOperation* operation;
operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:req
success:^(NSURLRequest *request, NSURLResponse *response, id JSON)
[[ordiniModel alloc] initWithDictionary:JSON];
failure:^(NSURLRequest *request, NSURLResponse *response, NSError
*error, id JSON)
[self setTitle:@"Dictionary"];
NSLog(@"failed! %d",[error code]);
];
[operation start];
ordiniModel*test;
NSLog(@"il valore è %@",test.ordini.description);
【问题讨论】:
你能说得更具体点吗?你得到什么回应?你有错误吗? 反应没问题!没有错误 【参考方案1】:AFJSONRequestOperation 是异步的,这意味着代码在应用程序的其余部分运行时继续执行。完成块在代码实际完成时运行。
那就试试吧:
NSLog(@"you are in a tableViewController");
self.title = @"NavigationOrdini";
ordiniModel *test; // <-- create variable here
NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.stampa6x3.com/json.php?azione=ordini"]];
AFJSONRequestOperation* operation;
operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:req
success:^(NSURLRequest *request, NSURLResponse *response, id JSON)
test = [[ordiniModel alloc] initWithDictionary:JSON]; // <-- Assign here
NSLog(@"il valore è %@",test.ordini.description);
failure:^(NSURLRequest *request, NSURLResponse *response, NSError
*error, id JSON)
[self setTitle:@"Dictionary"];
NSLog(@"failed! %d",[error code]);
];
[operation start];
【讨论】:
[测试 initWithDictionary:JSON]; //以上是关于Json 加速器和 AFNetworking的主要内容,如果未能解决你的问题,请参考以下文章