AFNetworking 2.0 对现有项目的更新

Posted

技术标签:

【中文标题】AFNetworking 2.0 对现有项目的更新【英文标题】:AFNetworking 2.0 update on an existing project 【发布时间】:2014-02-17 08:39:20 【问题描述】:

我最近将我的项目更新为 AFNetworking 2.0,并且似乎缺少 AFHTTPClient 文件。

我在以下网址阅读了以下内容:https://github.com/AFNetworking/AFNetworking/wiki/AFNetworking-2.0-Migration-Guide

看来我现在必须使用 AFHTTPRequestOperation 了?

如果是这样,我如何将以下代码修改为新的 AFHTTPRequestOperation?

NSURL *url = [NSURL URLWithString:WEB_SERVICE_BASE_URL];

        AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:url];
        [client registerHTTPOperationClass:[AFJSONRequestOperation class]];
        [client setDefaultHeader:@"Accept" value:@"application/json"];
        [client postPath:LOGIN_PATH parameters:[NSDictionary dictionaryWithObjectsAndKeys:self.userNameOrEmail.text ,@"userNameOrEmail",self.password.text,@"password", nil]
                 success:^(AFHTTPRequestOperation *operation, id responseObject) 
         
             bool success = [[responseObject valueForKeyPath:@"success"] boolValue];
             NSString *displayMessage = [responseObject valueForKeyPath:@"displaymessage"];

             if (success) 
                 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
                 [defaults setObject:self.userNameOrEmail.text forKey:@"userNameOrEmail"];
                 [defaults setObject:self.password.text forKey:@"password"];

  //              [Flurry setUserID:self.userNameOrEmail.text];
                 [SVProgressHUD showSuccessWithStatus:displayMessage];
                [self dismissViewControllerAnimated:YES completion:nil];
              else 
                 [SVProgressHUD showErrorWithStatus:displayMessage];
                 
          
                 failure:^(AFHTTPRequestOperation *operation, NSError *error) 
         

             NSLog(@"error is: %@", error);

             [SVProgressHUD showErrorWithStatus:MSG_SERVER_ERROR];
         ];

更新:我做了以下事情:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:self.userName.text ,@"user", self.email.text, @"email",self.password.text,@"password", nil];
        [manager POST:WEB_SERVICE_BASE_URL parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) 
            bool success = [[responseObject valueForKeyPath:@"success"] boolValue]? @"YES" : @"NO";

            NSString *displayMessage = [responseObject valueForKeyPath:@"displaymessage"];

            if (success) 
                [SVProgressHUD showSuccessWithStatus:displayMessage];
                [self.navigationController popToRootViewControllerAnimated:YES];
             else 
                [SVProgressHUD showErrorWithStatus:displayMessage];
            
         failure:^(AFHTTPRequestOperation *operation, NSError *error) 
            [SVProgressHUD showErrorWithStatus:MSG_SERVER_ERROR];
        ];

【问题讨论】:

您的更新看起来正确;它在工作吗?如果是这样,您应该将其发布为您自己问题的答案。 【参考方案1】:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:self.userName.text ,@"user", self.email.text, @"email",self.password.text,@"password", nil];
        [manager POST:WEB_SERVICE_BASE_URL parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) 
            bool success = [[responseObject valueForKeyPath:@"success"] boolValue]? @"YES" : @"NO";

            NSString *displayMessage = [responseObject valueForKeyPath:@"displaymessage"];

            if (success) 
                [SVProgressHUD showSuccessWithStatus:displayMessage];
                [self.navigationController popToRootViewControllerAnimated:YES];
             else 
                [SVProgressHUD showErrorWithStatus:displayMessage];
            
         failure:^(AFHTTPRequestOperation *operation, NSError *error) 
            [SVProgressHUD showErrorWithStatus:MSG_SERVER_ERROR];
        ];

【讨论】:

以上是关于AFNetworking 2.0 对现有项目的更新的主要内容,如果未能解决你的问题,请参考以下文章

从 AFNetworking 1.3 迁移到 AFNetworking 2.0 的问题

如何在旧的 AFNetworking 中使用 AFNetworking 2.0+?

使用 AFNetworking 2.0 对请求进行分组或批量处理

iOS Afnetworking 2.0 AfSecurityPolicy.m 在构建项目时显示错误

使用 AFNetworking 2.0 下载 PDF 文件

AFNetworking 2.0 JSON 解析