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