如何在afnetworking中获取AFHTTPRequestOperationManager请求的返回 [英] how to get return in AFHTTPRequestOperationManager request in afnetworking
问题描述
您好我在使用AFHTTPRequestOperationManager类发布数据,从服务器获取响应但无法返回数据。首先执行方法返回,然后成功数据即将到来我想获得值作为回报。
Hi i am posting data in using AFHTTPRequestOperationManager class getting the response from server but not able to return data . method return is executed first then success data is coming i want to get the value in return .
这是我的代码
.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类中调用此方法后导入此i我调用了这个
Now i am calling this method in my ViewController class after importing this i called like this
ServerRequest *servercall=[[ServerRequest alloc]init];
returninfo=[servercall getJsonData:nil actionmethod:@"loginuser?" parameters:inputs]
// here i want return data.
问题在这里没有回报。但在方法中我得到了。那么如何在成功后获取json数据请求,如何做到这一点
issue is here not getting return here . but in Method i am getting . so how to get json data after success Request , how to do this
推荐答案
您的请求方法使用块,这将不会立即执行,而是调度/调度,所以方法在请求完成之前返回(因此为零值)您可以重构您的方法以使用成功/错误块:
Your request method uses blocks, which will won't execute immediately, but instead get dispatched/scheduled, so the method returns before the request can complete (thus the nil value) You could refactor your method to use success/error blocks:
.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
}];
这篇关于如何在afnetworking中获取AFHTTPRequestOperationManager请求的返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!