gpt4 book ai didi

ios - 使用 AFNetworking 2 处理响应

转载 作者:行者123 更新时间:2023-11-29 03:10:10 26 4
gpt4 key购买 nike

我真的是 IOS 开发的新手。我想开发一个处理某些 Web 服务并在表格 View 中显示的应用程序。不知何故,我找到了一个用于网络工作的第三方库 [AFNetworking 2]。下面是我的代码,用于获取任何给定 url 和参数的 json 响应。

-(NSDictionary*)getWebServiceResponce:(NSString *)url :(NSDictionary *)object
{

// NSDictionary *parameters = [[NSDictionary alloc] initWithObjectsAndKeys:@"47", @"caregiverPersonId", nil];
__block NSDictionary* result=Nil;
__block NSString* person=Nil;


AFSecurityPolicy *policy = [[AFSecurityPolicy alloc] init];
[policy setAllowInvalidCertificates:YES];

AFHTTPRequestOperationManager *operationManager = [AFHTTPRequestOperationManager manager];
[operationManager setSecurityPolicy:policy];
operationManager.requestSerializer = [AFJSONRequestSerializer serializer];
operationManager.responseSerializer = [AFJSONResponseSerializer serializer];

[operationManager POST:url
parameters:object
success:^(AFHTTPRequestOperation *operation, id responseObject) {


NSLog(@"JSON: %@", [responseObject description]);
person = [responseObject[@"d"]objectForKey:@"PersonId"];
// [self returnedResponce:responseObject];
result = (NSDictionary *) responseObject[@"d"];
NSLog(@"RESULT: %@", result);
NSLog(@"personm: %@", person);
[operation waitUntilFinished];
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", [error description]);
//result = [error];
}
];

return result;
}

这段代码工作完美......但我的观点是,当我放置一些断点来检查我得到的几个变量的值时,它显示为空。但我的日志显示了整个 json 响应。

我想将我的响应对象作为字典返回。因为我想对响应进行一些处理..有人可以帮助我吗?

最佳答案

问题是 result 返回时为 nil。 AFNetworking 使用 ObjC 的 awesome block ,它们是异步执行的。 Read more about it here .

您应该在 getWebServiceResponce 方法中包含一个回调 block 。我整理了一些代码,但您真的应该阅读更多关于 block 的内容。

-(void)webServiceResponceForURL:(NSString *)url dictionary:(NSDictionary *)object success:(void (^)(NSDictionary *responseObject))success {
// NSDictionary *parameters = [[NSDictionary alloc] initWithObjectsAndKeys:@"47", @"caregiverPersonId", nil];
__block NSDictionary* result=Nil;
__block NSString* person=Nil;

AFSecurityPolicy *policy = [[AFSecurityPolicy alloc] init];
[policy setAllowInvalidCertificates:YES];

AFHTTPRequestOperationManager *operationManager = [AFHTTPRequestOperationManager manager];
[operationManager setSecurityPolicy:policy];
operationManager.requestSerializer = [AFJSONRequestSerializer serializer];
operationManager.responseSerializer = [AFJSONResponseSerializer serializer];
[operationManager POST:url
parameters:object
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", [responseObject description]);
person = [responseObject[@"d"]objectForKey:@"PersonId"];
// [self returnedResponce:responseObject];
result = (NSDictionary *) responseObject[@"d"];
NSLog(@"RESULT: %@", result);
NSLog(@"personm: %@", person);

//We are executing the block as soon as we have the results.
if (success) {
success(responseObject);
}
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", [error description]);
//result = [error];
}
];
}

编辑:

[self webServiceResponceForURL:@"foo://foo" dictionary:nil success:^(NSDictionary *responseObject) {
//your code here
}

关于ios - 使用 AFNetworking 2 处理响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22323686/

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