how to get return in AFHTTPRequestOperationManager request in afnetworking

限于喜欢 提交于 2019-12-04 20:03:19
József Vesza

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 file

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

.m file

-(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);
     }];
}

Then later:

ServerRequest *servercall=[[ServerRequest alloc] init];
[servercall getJsonData:nil actionmethod:@"loginuser?" parameters:inputs onComplete:^(NSDictionary *json) {

    // return json ehre
} onError:^(NSError *error) {

    // handle error here
}];
ChenSmile

You can try like this also -

.h file

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

.m file

-(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;
}

Call this method in ViewController class

-(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
}];
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!