我对IOS开发真的很陌生。我想开发一个应用程序,它处理一些Web服务并在表视图中显示。不知何故,我找到了第三方库来做网络的东西[AFNetworking 2]。下面是我获取任何给定网址和参数的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;
}
这段代码完美无缺..但我的观点是,当我放置一些断点来检查我为几个变量得到的值时,它显示为null。但是我的日志显示了整个json响应。
我希望将我的响应对象作为字典返回。因为我想用回复做一些过程..有人可以帮我这个吗?
答案 0 :(得分:1)
问题是result
在返回时为零。 AFNetworking使用ObjC的强大块,它们以异步方式执行。 Read more about it here
您应该在getWebServiceResponce
方法中包含一个回调块。我把一些代码放在一起,但你应该真正阅读更多关于块的内容。
-(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
}
答案 1 :(得分:0)
[self webServiceResponceForURL:@"foo://foo" dictionary:nil success:^(NSDictionary *responseObject) {
//your code here
}
在这里,您将获得NSDictionary格式的完整responseObject。您可以将responseObject分配给实例变量。现在,此实例变量将在点时使用。在你的情况下,它将传递按钮事件。