OStack程序员社区-中国程序员成长平台

标题: ios - AFNetworking 2.0 对现有项目的更新 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 18:55
标题: ios - AFNetworking 2.0 对现有项目的更新

我最近将我的项目更新为 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 postPathOGIN_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];
        }];



Best Answer-推荐答案


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];
        }];

关于ios - AFNetworking 2.0 对现有项目的更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21824265/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4