AFNetworking 2.0 HTTP POST 进展

Posted

技术标签:

【中文标题】AFNetworking 2.0 HTTP POST 进展【英文标题】:AFNetworking 2.0 HTTP POST Progress 【发布时间】:2014-02-22 08:19:26 【问题描述】:

如何获得 AFHTTPRequest 的进度?我试过在网上搜索。 我正在使用:

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSDictionary *params = @@"gameId" : datas[0], @"p1": datas[1], @"p2":datas[2], @"turn":datas[3] ;
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:@"http://localhost/thepath/isprivate/thefile.php"
       parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) 

     failure:^(AFHTTPRequestOperation *operation, NSError *error) 
    ];

是否有我可以用来访问 AFNetworking 2.0 HTTP POST 进度的属性或方法?

【问题讨论】:

您好,您可以查看此答案***.com/a/20543730/1228669 【参考方案1】:

试试这个:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *params = @@"gameId" : datas[0], @"p1": datas[1], @"p2":datas[2], @"turn":datas[3] ;
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];

NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:@"http://localhost/thepath/isprivate/thefile.php" parameters:params error:&error];

AFHTTPRequestOperation *requestOperation = [manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) 
    NSLog(@"operation success: %@\n %@", operation, responseObject);
 failure:^(AFHTTPRequestOperation *operation, NSError *error) 
    NSLog(@"Error: %@", error);
];

[requestOperation setUploadProgressBlock:^(NSUInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) 
    double percentDone = (double)totalBytesWritten / (double)totalBytesExpectedToWrite;
    NSLog(@"progress updated(percentDone) : %f", percentDone);
];
[requestOperation start];

希望这会有所帮助。

【讨论】:

谢谢,但 Avt 最早回复。 +1 给你,谢谢。【参考方案2】:

实现这一点的最短方法是

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    AFHTTPRequestOperation *requestOperation = [manager POST:@"/service/user/authenticate" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) 
        //Your Business Operation.
        NSLog(@"operation success: %@\n %@", operation, responseObject);
     failure:^(AFHTTPRequestOperation *operation, NSError *error) 
        NSLog(@"Error: %@", error);
    ];
//Here is the upload progress
[requestOperation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) 
            double percentDone = (double)totalBytesWritten / (double)totalBytesExpectedToWrite;
            //Upload Progress bar here
            NSLog(@"progress updated(percentDone) : %f", percentDone);
        ];

【讨论】:

看起来您正在成功块内设置进度块。这真的有效吗?我认为你基本上是在成功完成后设置进度块是没有用的。 @SterlingChristensen 感谢您的通知。代码已被适当地编辑。谢谢【参考方案3】:

您可以使用以下方法

- (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request
                                         fromData:(NSData *)bodyData
                                         progress:(NSProgress * __autoreleasing *)progress
                                completionHandler:(void (^)(NSURLResponse *response, id responseObject, NSError *error))completionHandler

属于AFHTTPSessionManager 类。

更新:

通常您会更喜欢使用 KVO 来获取上传值。所以应该使用类似下面的东西:

static void * kDGProgressChanged = &kDGProgressChanged;

...

[progress addObserver:self 
           forKeyPath:@"fractionCompleted" 
              options:NSKeyValueObservingOptionNew 
              context:kDGProgressChanged];

...

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(__unused NSDictionary *)change
                       context:(void *)context

    if (kDGProgressChanged == context) 
        dispatch_async(dispatch_get_main_queue(), ^
            [self updateProgressInfo];
        );
    

【讨论】:

我使用了这个方法但是没有调用进度。上传操作成功完成,虽然我使用 KVO 进行监控,但文件已上传但未跟踪进度。会发生什么?

以上是关于AFNetworking 2.0 HTTP POST 进展的主要内容,如果未能解决你的问题,请参考以下文章

AFNetworking 2.0 多部分请求正文空白

AFNetworking 2.0 中的 AFHTTPRequestOperationManager 如何传递 HTTPBody 标签

设置身份验证。令牌头 AFNetworking 2.0

Swift 中 AFNetworking 2.0 中的 Web 服务调用问题

iOS Afnetworking 2.0 AfSecurityPolicy.m 在构建项目时显示错误

“请求失败:不可接受的内容类型:文本/html”使用 AFNetworking 2.0