Search code examples
iosobjective-ciphoneafnetworking-2

how to get return in AFHTTPRequestOperationManager request in afnetworking


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 .

this is my code
.h file

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

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.

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


Solution

  • 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
    }];