简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM