0

我可以将图像发送到服务器,但我遇到了问题。我可以用这样的简单块检查失败:

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"success: %@", operation.responseString);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"error: %@",  operation.responseString);
    }
 ];
[operation start];

但是我看不到这个块的发送进度。所以我找到了一个带有进度回调的特殊块,如下所示:

[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
    NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];
[httpClient enqueueHTTPRequestOperation:operation];

问题是 setUploadProgressBlock 没有“失败:” ...

所以我的问题是......有一种方法可以检查发送是否失败?

谢谢你的帮助

4

2 回答 2

1

这将正常工作,它会取得进展,如果出现问题,也会进入失败的块:

NSURLRequest *request = [[NSURLRequest alloc] init];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
 NSLog(@"success: %@", operation.responseString);
 }
 failure:^(AFHTTPRequestOperation *operation, NSError *error) {
 // If access token is null , we ask to the user if he wants to sign in or sign up ????
 NSLog(@"error: %@",  operation.responseString);
 }
 ];
 [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
 NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
 }];
[operation start];
于 2013-05-30T16:06:20.660 回答
0

您可以设置两个块并实现您想要的。

于 2013-05-30T16:02:36.613 回答