gpt4 book ai didi

ios - 使用 AFNetworking 进行异步 POST json 请求

转载 作者:可可西里 更新时间:2023-11-01 04:16:17 24 4
gpt4 key购买 nike

我一直在尝试使用 AFNetworking 来使用 POST json。经过一些研究,我终于让这个工作了

- (IBAction)go:(id)sender {
//Some POST
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"https://www.test.com/"]];
[httpClient setParameterEncoding:AFJSONParameterEncoding];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST"
path:@"https://www.test.com/user/login/"
parameters:@{@"username":self.username.text, @"password":self.password.text}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
// Print the response body in text
NSDictionary *jsonDict = (NSDictionary *) responseObject;
BOOL *success = [[jsonDict objectForKey:@"success"] boolValue];
NSLog(@"Response: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
[operation start];
}

这对于演示目的来说很好,但现在我想最终使用 NSDirectory 来拆分我的 json 对象,所以我尝试做这样的事情,但是每当我点击按钮时我的应用程序崩溃

- (IBAction)go:(id)sender {
//Some POST
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"https://www.test.com/"]];
[httpClient setParameterEncoding:AFJSONParameterEncoding];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST"
path:@"https://www.test.com/user/login/"
parameters:@{@"username":self.username.text, @"password":self.password.text}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
// Print the response body in text
NSDictionary *jsonDict = (NSDictionary *) responseObject;
BOOL success = [[jsonDict objectForKey:@"success"] boolValue];
if (success) {
NSLog(@"yes!!!");
}else{
NSString *reason = [jsonDict objectForKey:@"reason"];
NSLog(@"reason: %@",reason);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
[operation start];
}

错误

2013-03-08 03:27:34.648 test[18253:c07] -[__NSCFData objectForKey:]: unrecognized selector sent to instance 0x888a6b0
2013-03-08 03:27:34.648 test[18253:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFData objectForKey:]: unrecognized selector sent to instance 0x888a6b0'
*** First throw call stack:
(0x1ce0012 0x111de7e 0x1d6b4bd 0x1ccfbbc 0x1ccf94e 0x24608 0x12e71 0x4a4453f 0x4a56014 0x4a467d5 0x1c86af5 0x1c85f44 0x1c85e1b 0x1c3a7e3 0x1c3a668 0x61ffc 0x21ad 0x20d5)
libc++abi.dylib: terminate called throwing an exception

最佳答案

您正在使用 AFHTTPRequestOperation 而不是 AFJSONRequestOperation

您也可以直接使用AFHttpClient:

NSDictionary *parameter = @{@"username":self.username.text, @"password":self.password.text};
[httpClient setParameterEncoding:AFJSONParameterEncoding];
[httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];

[httpClient postPath:@"api/v1/user/login/" parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
// Print the response body in text
BOOL *success = [[responseObject objectForKey:@"success"] boolValue];
NSLog(@"Response: %@",responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[self handleConnectionError:error];
}];

我还建议使用 clientWithBaseURL: 方法只创建一个 AFHTTPClient 实例。

关于ios - 使用 AFNetworking 进行异步 POST json 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15290761/

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