Reputation:
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
Upvotes: 1
Views: 3464
Reputation: 3441
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
}];
}
Upvotes: 1
Reputation: 4795
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
}];
Upvotes: 2