AFNetworking POST方法调用
我是AFNetworking
。 我如何执行以下的成功和失败块
NSMutableDictionary *rus = [[NSMutableDictionary alloc] init]; [rus setValue:@"1211" forKey:@"id"]; [rus setValue:@"33" forKey:@"man"]; [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:@"http://mysite.co/service.php" rus];
如何在执行上述代码片段后显示成功或失败。
requestWithMethod实际上并没有完成调用,它只是返回一个NSMutableURLRequest。 您需要接受该请求并进行AFHTTPRequestOperation。 您可以在此对象上设置成功/失败块,然后运行start方法。
NSMutableDictionary *rus = [[NSMutableDictionary alloc] init]; [rus setValue:@"1211" forKey:@"id"]; [rus setValue:@"33" forKey:@"man"]; NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:@"http://mysite.co/service.php" parameters:rus]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { // Code for success } failure:^(AFHTTPRequestOperation *operation, NSError *error) { //Code for failure }]; [operation start];
所有这一切,你是否使用AFNetworking的过时版本? 您正在引用的方法已被弃用。
- (NSMutableURLRequest *)requestWithMethod:(NSString *)method URLString:(NSString *)URLString parameters:(id)parameters error:(NSError *__autoreleasing *)error
以上是使用正确的方法。 你应该传入一个错误对象。
最后,一个更简单的方法来完成你正在做的与AFNetworking 2.0是如下:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; [manager POST:@"http://mysite.co/service.php" parameters:rus success:^(AFHTTPRequestOperation *operation, id responseObject) { // Code for success } failure:^(AFHTTPRequestOperation *operation, NSError *error) { // Code for failure }];
试试这个代码:
NSMutableDictionary *rus = [[NSMutableDictionary alloc] init]; [rus setValue:@"1211" forKey:@"id"]; [rus setValue:@"33" forKey:@"man"]; AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; [manager POST:@"http://mysite.co/service.php" parameters:rus success:^(AFHTTPRequestOperation *operation, id responseObject) { } failure:^(AFHTTPRequestOperation *operation, NSError *error) { }];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; [manager POST:@"http://mysite.co/service.php" parameters:rus success:^(AFHTTPRequestOperation *operation, id responseObject) { // Code for success } failure:^(AFHTTPRequestOperation *operation, NSError *error) { // Code for failure }];
github的AFNetworking文件