gpt4 book ai didi

ios - 如何在afnetworking中的AFHTTPRequestOperationManager请求中获得返回

转载 作者:行者123 更新时间:2023-12-01 18:52:52 24 4
gpt4 key购买 nike

嗨,我正在使用 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数据,如何做到这一点

最佳答案

您的请求方法使用 block ,它不会立即执行,而是被调度/调度,因此该方法在请求完成之前返回(因此 nil 值)您可以重构您的方法以使用成功/错误 block :

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

关于ios - 如何在afnetworking中的AFHTTPRequestOperationManager请求中获得返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29794028/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com