gpt4 book ai didi

ios - AFNetworking:创建 POST 请求,但不运行

转载 作者:行者123 更新时间:2023-11-28 21:58:38 24 4
gpt4 key购买 nike

我正在尝试在 AFNetworking 2.0 中创建 POST 请求:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];

AFHTTPRequestOperation *operation = [manager POST: requestURL parameters:params
success:^(AFHTTPRequestOperation *operation, id responseObject) {

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

}];

但是我如何创建 AFHTTPRequestOperation 而不立即执行呢?(我需要将这个操作添加到 NSOperationQueue )

更新:

谢谢!我最终得到了这样的解决方案:

 NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] requestWithMethod: @"POST" URLString: @"website" parameters: params error: nil];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

}];

[operation start]; // or add operation to queue

最佳答案

直接来自 docs

NSURL *URL = [NSURL URLWithString:@"http://example.com/foo.json"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

operation.responseSerializer = [AFJSONResponseSerializer serializer];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"%@", responseObject);
} failure:nil];

[operation start];

编辑

如果要将这个操作提交到NSOperationQueue,可以使用AFHTTPRequestOperationManager如下

requestManager = [AFHTTPRequestOperationManager manager];

// instead of [operation start]
[requestManager.operationQueue addOperation:operation];

关于ios - AFNetworking:创建 POST 请求,但不运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25738232/

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