您好我在使用AFHTTPRequestOperationManager类发布数据,从服务器获取响应但无法返回数据。首先执行方法返回然后成功数据即将到来我想要获得值作为回报。
这是我的代码
.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类中调用了这个方法,导入后我调用了这个
ServerRequest *servercall=[[ServerRequest alloc]init];
returninfo=[servercall getJsonData:nil actionmethod:@"loginuser?" parameters:inputs]
// here i want return data.
问题在这里没有得到回报。但在方法我得到。那么如何在成功后获取json数据请求,如何做到这一点
答案 0 :(得分:2)
您的请求方法使用块,它将不会立即执行,而是被调度/调度,因此该方法在请求完成之前返回(因此为零值)您可以重构您的方法以使用成功/错误块:
.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
}];