如何在AFnetworking中的AFHTTPRequestOperationManager请求中返回

嗨我发布数据使用AFHTTPRequestOperationManager类获取来自服务器的响应,但无法返回数据。 方法返回是先执行然后成功的数据来了,我想要得到的价值作为回报。

这是我的代码
.h文件

@interface ServerRequest : NSObject { } -(NSString *) JsonData:(NSString *)newparams actionmethod:(NSString *)action parameters:(NSDictionary *)params; 

.M

 #import "ServerRequest.h" #import "AFNetworking.h" @implementation ServerRequest { } -(NSDictionary *) getJsonData:(NSString *)anynewparams actionmethod:(NSString *)action parameters:(NSDictionary *)params { NSMutableDictionary *json = [[NSMutableDictionary alloc] init]; NSString *url = @"http://gjkhdhdyi/ghdgd/Rest/"; url = [url stringByAppendingString:action]; AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer]; [requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; [requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"]; manager.requestSerializer = requestSerializer; [manager POST:weburl parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"JSON: %@", responseObject); json=responseObject; // here i am getting data } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error); }]; return json; } 

现在我调用这个方法在我的ViewController类导入后,我这样调用

 ServerRequest *servercall=[[ServerRequest alloc]init]; returninfo=[servercall getJsonData:nil actionmethod:@"loginuser?" parameters:inputs] // here i want return data. 

问题在这里没有得到回报。 但在方法我越来越。 那么如何在成功请求后获取json数据,如何做到这一点

你的请求方法使用块,它不会立即执行,而是被调度/调度,所以方法在请求完成之前返回(因此nil值)你可以重构你的方法来使用成功/错误块:

.h文件

 -(void)getJsonData:(NSString *)anynewparams actionmethod:(NSString *)action parameters:(NSDictionary *)params onComplete:(void (^)(NSDictionary *json))successBlock onError:(void (^)(NSError *error))errorBlock; 

.m文件

 -(void)getJsonData:(NSString *)anynewparams actionmethod:(NSString *)action parameters:(NSDictionary *)params onComplete:(void (^)(NSDictionary *json))successBlock onError:(void (^)(NSError *error))errorBlock { NSString *url = @"http://gjkhdhdyi/ghdgd/Rest/"; url = [url stringByAppendingString:action]; AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer]; [requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; [requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"]; manager.requestSerializer = requestSerializer; [manager POST:weburl parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"JSON: %@", responseObject); successBlock(responseObject); } failure: ^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error); errorBlock(error); }]; } 

后来:

 ServerRequest *servercall=[[ServerRequest alloc] init]; [servercall getJsonData:nil actionmethod:@"loginuser?" parameters:inputs onComplete:^(NSDictionary *json) { // return json ehre } onError:^(NSError *error) { // handle error here }]; 

你也可以尝试这样 –

.h文件

 - (NSDictionary *) getJsonData:(NSString *)anynewparams actionmethod:(NSString *)action parameters:(NSDictionary *)params onComplete:(void (^)(NSDictionary *json))successBlock onError:(void (^)(NSError *error))errorBlock; 

.m文件

 -(NSDictionary *) getJsonData:(NSString *)anynewparams actionmethod: (NSString *)action parameters:(NSDictionary *)params onComplete:(void (^)(NSDictionary *json))successBlock onError:(void (^)(NSError *error))errorBlock{ __block id json; NSString *url = @"http://gjkhdhdyi/ghdgd/Rest/"; url = [url stringByAppendingString:action]; AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer]; [requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; [requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"]; manager.requestSerializer = requestSerializer; [manager POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { successBlock(responseObject); } failure: ^(AFHTTPRequestOperation *operation, NSError *error){ NSLog(@"Error: %@", error); }]; return json; } 

在ViewController类中调用此方法

 -(void)call_LoginWebService{ returninfo=[[NSDictionary alloc]init]; BaseRequest *basecall=[[BaseRequest alloc]init]; [basecall getJsonData:nil actionmethod:@"LoginUser?" parameters:inputs onComplete:^(NSDictionary *json) { NSLog(@"alll data here ==%@",json); returninfo = json; } onError:^(NSError *error) { // handle error here }]; }