2014-02-17 74 views
2

我最近將項目更新爲AFNetworking的2.0,看起來AFHTTPClient文件丟失。對現有項目的AFNetworking 2.0更新

我看了在以下幾點: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]; 
     }]; 
+0

你更新看起來是正確的;它工作嗎?如果是這樣,你應該發佈它作爲你自己問題的答案。 –

回答

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