gpt4 book ai didi

ios - 现有项目的 AFNetworking 2.0 更新

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:36:28 26 4
gpt4 key购买 nike

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

最佳答案

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com